# 1759 암호 만들기
def check(input_val,vowel):
check_list = [0,0]
for val in input_val:
if val in vowel:
check_list[0] += 1
else:
check_list[1] += 1
if check_list[0] >= 1 and check_list[1] >= 2:
return True
else:
return False
def dfs(cnt,ind,result):
global vowel
if ind == C:
if cnt == L and check(result,vowel):
total.append(result)
return
else:
dfs(cnt+1,ind+1,result+input_list[ind])
dfs(cnt,ind+1,result)
L,C = map(int,input().split())
vowel = 'aeiou'
input_list = list(input().split())
input_list.sort()
total = list()
dfs(0,0,'')
for val in total:
print(val)
재귀를 이용해서 조합을 만들어준 것과 같다. 각 인덱스에 들어와서, 해당 문자를 포함하거나 포함하지 않는 경우를 구분해서 재귀를 돌려주었다.
그리고 주어진 길이만큼 되면, 그 안에 모음과 자음 숫자에 따라 True False를 반환해주고, True일때 결과값에 추가해줬다.
from itertools import combinations
L,C = map(int,input().split())
input_value = list(input().split())
input_value.sort()
vowel = set(['a','e','i','o','u'])
for combi in combinations(input_value,L):
password = set(combi)
if password & vowel:
if len(password-vowel)>=2:
print(''.join(combi))
좀 더 간단한 풀이이다. 위에서 말했듯이 이것은 조합을 구하는것과 동일하다.
combinations 라이브러리를 활용해 전체 combinations을 구하고, 집합의 특성을 이용해, 교집합이 존재하는지, 차집합을 통해 길이가 2이상인지 구분해줘서, 만약 두개다 통과하다면, 원하는 값이므로 출력해주는 방식으로 풀었다.
'알고리즘 > 백준' 카테고리의 다른 글
[BOJ/백준] 1937 욕심쟁이 판다 (0) | 2021.02.16 |
---|---|
[BOJ/백준] 9466 텀 프로젝트 (0) | 2021.02.15 |
[BOJ/백준] 1991 트리 순회 (0) | 2021.02.15 |
[BOJ/백준] 11047 동전 0 (0) | 2021.02.15 |
[BOJ/백준] 13549 숨바꼭질 3 (0) | 2021.02.13 |