Skip to content

Commit bcd4d65

Browse files
committed
feat: 발제 문제 & 과제 문제 정답 코드 업데이트
1 parent c5400e3 commit bcd4d65

File tree

3 files changed

+112
-86
lines changed

3 files changed

+112
-86
lines changed
Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,7 @@
1-
'''
2-
BOJ #1717. 집합의 표현 (골드5)
3-
https://www.acmicpc.net/problem/1717
4-
유형: Union-Find, Graph
5-
'''
1+
"""
2+
BOJ #6603. 로또 (실버2)
3+
https://www.acmicpc.net/problem/6603
4+
유형: Graph, Backtracking
5+
"""
66

7-
import sys
8-
sys.setrecursionlimit(10 ** 6) # 재귀 깊이 제한 늘리기
9-
input = sys.stdin.readline
10-
11-
# Union 연산(두 집합을 합치기 위한 함수)
12-
def union(a, b):
13-
p_a = find(a)
14-
p_b = find(b)
15-
16-
if p_a > p_b: # 값이 더 작은 쪽을 부모로 설정
17-
parent[p_a] = p_b
18-
else:
19-
parent[p_b] = p_a
20-
21-
# Find 연산(같은 집합에 속하는지 확인하기 위한 함수)
22-
def find(a):
23-
if a != parent[a]:
24-
parent[a] = find(parent[a]) # 경로 압축
25-
return parent[a]
26-
27-
# 연산 수행
28-
n, m = map(int, input().split())
29-
parent = [i for i in range(n + 1)] # 초기: 각 원소가 자기 자신을 부모로 가짐
30-
31-
for _ in range(m):
32-
flag, a, b = map(int, input().split())
33-
if flag == 0: # 합집합 연산
34-
union(a, b)
35-
else: # 같은 집합 확인 연산
36-
if find(a) == find(b):
37-
print("YES")
38-
else:
39-
print("NO")
7+
# 토요일에 업로드 예정
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
BOJ #1759. 암호 만들기 (골드5)
3+
https://www.acmicpc.net/problem/1759
4+
유형: Graph, Backtracking
5+
"""
6+
7+
"""
8+
풀이1: 백트래킹
9+
"""
10+
import sys
11+
input = sys.stdin.readline
12+
13+
L, C = map(int, input().split()) # L: 암호 길이, C: 문자 종류
14+
chars = sorted(input().split()) # 사전순 정렬
15+
vowels = {'a', 'e', 'i', 'o', 'u'}
16+
17+
18+
def is_valid(word):
19+
# 최소 한 개의 모음과 최소 두 개의 자음으로 구성되어있는지 확인
20+
vowel_cnt, consonant_cnt = 0, 0 # 모음 개수, 자음 개수
21+
for w in word:
22+
if w in vowels:
23+
vowel_cnt += 1
24+
else:
25+
consonant_cnt += 1
26+
27+
return vowel_cnt >= 1 and consonant_cnt >= 2
28+
29+
30+
def backtrack(word, start):
31+
if len(word) == L: # 종료 조건
32+
if is_valid(word):
33+
print(''.join(word))
34+
return
35+
36+
for i in range(start, C):
37+
word.append(chars[i])
38+
backtrack(word, i+1)
39+
word.pop()
40+
41+
backtrack([], 0)
42+
43+
44+
"""
45+
풀이2: 조합
46+
출처: https://velog.io/@dlgosla/%EB%B0%B1%EC%A4%80-BOJ-%EC%95%94%ED%98%B8-%EB%A7%8C%EB%93%A4%EA%B8%B01759-python
47+
"""
48+
from itertools import combinations
49+
50+
L, C = map(int, input().split())
51+
52+
alphabets = input().split()
53+
54+
# 길이가 L인 모든 조합, 증가하는 순서로 배열해야되기 때문에 sort 후 comb
55+
alpha_combs = combinations(sorted(alphabets), L)
56+
57+
answer = []
58+
59+
for alpha_comb in alpha_combs: # 가능한 조합 중에서
60+
consonant_count = 0
61+
vowel_count = 0
62+
63+
# 자음 모음 개수 세기
64+
for alpha in alpha_comb:
65+
if alpha in "aeiou":
66+
consonant_count += 1
67+
else:
68+
vowel_count += 1
69+
70+
# 모음이 1개 이상, 자음이 2 개 이상이면 출력
71+
if consonant_count >= 1 and vowel_count >= 2:
72+
print("".join(alpha_comb))
73+
74+
"""
75+
풀이3: DFS 재귀 방식
76+
출처: https://velog.io/@dlgosla/%EB%B0%B1%EC%A4%80-BOJ-%EC%95%94%ED%98%B8-%EB%A7%8C%EB%93%A4%EA%B8%B01759-python
77+
"""
78+
L, C = map(int, input().split())
79+
80+
alphabets = sorted(input().split())
81+
82+
83+
def dfs(idx, codes):
84+
if L == idx:
85+
vowel_count = 0
86+
consonant_count = 0
87+
88+
# 자음 모음 개수 세기
89+
for code in codes:
90+
if code in "aeiou":
91+
consonant_count += 1
92+
else:
93+
vowel_count += 1
94+
95+
# 자음 2개 이상, 모음 한개 이상이면 암호가 될 수 있으므로 출력
96+
if consonant_count >= 1 and vowel_count >= 2:
97+
print("".join(codes))
98+
99+
else:
100+
for i in range(idx, C):
101+
if codes and alphabets[i] <= codes[-1]: # 오름차순 아니면 버림
102+
continue
103+
dfs(idx + 1, codes + [alphabets[i]])
104+
105+
106+
dfs(0, [])

_WeeklyChallenges/W19-[Graph-Backtracking]/Study_BOJ_1759_암호만들기py

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)