Etc/알고리즘
[백준] 1759번(암호 만들기) with Python
철갑
2020. 10. 13. 22:16
728x90
문제
1759번: 암호 만들기
첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.
www.acmicpc.net
풀이
배열을 정렬한 후 itertools 라이브러리에서 combinations 이터레이터를 사용해서 문제를 풀었다.
그리고 최소 한 개의 모음(a, e, i, o, u)과 최소 두 개의 자음으로 구성이 되었는지 카운트 해서 결과를 도출했다.
docs.python.org/ko/3/library/itertools.html#itertools.combinations
itertools — 효율적인 루핑을 위한 이터레이터를 만드는 함수 — Python 3.9.0 문서
docs.python.org

답
import sys
from itertools import combinations
vowels = ['a', 'e', 'i', 'o', 'u']
L, C = map(int,input().split(' '))
pwd = sorted(input().split(' ')[:C])
comb = combinations(pwd, L)
for c in comb:
cnt = 0
for r in c:
if r in vowels:
cnt += 1
if (cnt >= 1) and (cnt <= L-2):
print(''.join(c))

728x90