알고리즘/백준
[백준] 1759번, 암호 만들기 (Python)
wookcode
2021. 8. 13. 15:42
728x90
반응형
문제
https://www.acmicpc.net/problem/1759
1759번: 암호 만들기
첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.
www.acmicpc.net
코드
from itertools import combinations
l, c = map(int, input().split())
alpha = list(input().split())
mo = ['a','e','i','o','u']
answer = []
result = list(combinations(alpha, l))
for i in result:
i = list(i)
i.sort()
count = 0
for j in mo:
if j in i:
count += 1
if 1 <= count <= l-2:
ret = ''.join(i)
answer.append(ret)
answer.sort()
for i in answer:
print(i)
알파벳 순으로 L개수만큼 조합하여 출력하는 문제로 combinations를 사용하여 조합을 만들어주고
모음은 1개이상 들어가야하고 자음은 2개이상 들어간 조합이어야 한다.
모음 리스트로 count를 세서 모음수가 1이상 L-2(자음이 들어갈 수는 남겨야함) 까지만 answer리스트에 넣어준다.
반응형