Skip to content

Commit 543215d

Browse files
authored
Merge pull request #198 from AlgorithmStudy-Allumbus/minjeong3
Minjeong / 4월 3주차 / 4문제
2 parents ecdaef5 + 97f454c commit 543215d

File tree

6 files changed

+194
-1
lines changed

6 files changed

+194
-1
lines changed

_WeeklyChallenges/W19-[Graph-Backtracking]/Assignment_BOJ_6603_로또.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,49 @@
44
유형: Graph, Backtracking
55
"""
66

7-
# 토요일에 업로드 예정
7+
"""
8+
#6603. 로또
9+
백트래킹 풀이
10+
"""
11+
import sys
12+
input = sys.stdin.readline
13+
def backtrack(lotto, current):
14+
if len(lotto) == 6: # 종료조건
15+
print(' '.join(map(str, lotto)))
16+
return
17+
18+
for i in range(current, k):
19+
lotto.append(S[i])
20+
backtrack(lotto, i+1)
21+
lotto.pop()
22+
23+
24+
while True:
25+
testcase = input().strip()
26+
if testcase == '0':
27+
break
28+
nums = list(map(int, testcase.split()))
29+
k, S = nums[0], nums[1:]
30+
31+
backtrack([], 0)
32+
print()
33+
34+
35+
"""
36+
#6603. 로또
37+
조합 풀이
38+
"""
39+
import sys
40+
from itertools import combinations
41+
input = sys.stdin.readline
42+
43+
while True:
44+
testcase = input().strip()
45+
if testcase == '0':
46+
break
47+
nums = list(map(int, testcase.split()))
48+
k, S = nums[0], nums[1:]
49+
combs = list(combinations(S, 6))
50+
for comb in combs:
51+
print(' '.join(map(str, comb)))
52+
print()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
"""
5+
#6603. 로또
6+
백트래킹 풀이
7+
"""
8+
9+
def backtrack(lotto, current):
10+
if len(lotto) == 6: # 종료조건
11+
print(' '.join(map(str, lotto)))
12+
return
13+
14+
for i in range(current, k):
15+
lotto.append(S[i])
16+
backtrack(lotto, i+1)
17+
lotto.pop()
18+
19+
20+
while True:
21+
testcase = input().strip()
22+
if testcase == '0':
23+
break
24+
nums = list(map(int, testcase.split()))
25+
k, S = nums[0], nums[1:]
26+
27+
backtrack([], 0)
28+
print()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sys
2+
from itertools import combinations
3+
input = sys.stdin.readline
4+
5+
"""
6+
#6603. 로또
7+
조합 풀이
8+
"""
9+
10+
while True:
11+
testcase = input().strip()
12+
if testcase == '0':
13+
break
14+
nums = list(map(int, testcase.split()))
15+
k, S = nums[0], nums[1:]
16+
combs = list(combinations(S, 6))
17+
for comb in combs:
18+
print(' '.join(map(str, comb)))
19+
print()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
L, C = map(int, input().split()) # L: 암호 길이, C: 문자 종류
5+
chars = sorted(input().split()) # 사전순 정렬
6+
vowels = {'a', 'e', 'i', 'o', 'u'}
7+
8+
9+
def is_valid(word):
10+
# 최소 한 개의 모음과 최소 두 개의 자음으로 구성되어있는지 확인
11+
vowel_cnt, consonant_cnt = 0, 0 # 모음 개수, 자음 개수
12+
for w in word:
13+
if w in vowels:
14+
vowel_cnt += 1
15+
else:
16+
consonant_cnt += 1
17+
18+
return vowel_cnt >= 1 and consonant_cnt >= 2
19+
20+
21+
def backtrack(word, start):
22+
if len(word) == L: # 종료 조건
23+
if is_valid(word):
24+
print(''.join(word))
25+
return
26+
27+
for i in range(start, C):
28+
word.append(chars[i])
29+
backtrack(word, i+1)
30+
word.pop()
31+
32+
backtrack([], 0)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
T = int(input())
5+
for _ in range(T):
6+
N = int(input()) # 동전의 가지 수
7+
coins = list(map(int, input().split())) # N가지 동전의 각 금액
8+
M = int(input()) # 목표 금액
9+
10+
dp = [0] * (M+1)
11+
dp[0] = 1
12+
13+
for coin in coins:
14+
for i in range(coin, M + 1):
15+
dp[i] += dp[i - coin]
16+
17+
print(dp[M])
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
5+
def min_ops_to_pal(s):
6+
"""
7+
s: list of chars
8+
return: 최소 편집 연산(삽입, 삭제, 교체만 허용)으로 s를 팰린드롬으로 만드는 비용
9+
"""
10+
n = len(s)
11+
# dp[i][j]: s[i..j]를 팰린드롬으로 만드는 최소 연산 횟수
12+
dp = [[0] * n for _ in range(n)]
13+
# 길이 2부터 n까지 늘려가며
14+
for length in range(2, n + 1):
15+
for i in range(n - length + 1):
16+
j = i + length - 1
17+
if s[i] == s[j]:
18+
dp[i][j] = dp[i + 1][j - 1]
19+
else:
20+
# 교체(대칭 맞추기), 삭제(왼쪽), 삭제(오른쪽)
21+
dp[i][j] = min(
22+
dp[i + 1][j - 1] + 1,
23+
dp[i + 1][j] + 1,
24+
dp[i][j - 1] + 1
25+
)
26+
for d in dp:
27+
print(d)
28+
29+
return dp[0][n - 1] if n > 0 else 0
30+
31+
32+
s = list(input().rstrip())
33+
n = len(s)
34+
35+
# 1. 교환 없이 삽입/삭제/교체만 사용했을 때
36+
ans = min_ops_to_pal(s)
37+
if ans <= 1:
38+
print(ans)
39+
exit()
40+
41+
# 2. 서로 다른 문자끼리 한 번만 교환을 허용해봤을 때, 교환 비용 1을 더해보고 개선되는지 확인
42+
for i in range(n):
43+
for j in range(i + 1, n):
44+
if s[i] != s[j]:
45+
# 문자가 같지 않은 경우, SWAP
46+
s[i], s[j] = s[j], s[i]
47+
swap_cost = min_ops_to_pal(s) + 1 # swap 비용 포함
48+
if swap_cost < ans: # swap한 경우가 더 작다면 ans에 업데이트
49+
ans = swap_cost
50+
# 복구
51+
s[i], s[j] = s[j], s[i]
52+
print(ans)

0 commit comments

Comments
 (0)