백준 알고리즘

[백준/Python]1759_암호 만들기

헝그리개발자 2021. 6. 17. 15:03

문제

https://www.acmicpc.net/problem/1759

 

1759번: 암호 만들기

첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.

www.acmicpc.net

풀이

\( _{C}\mathrm{C}_{L} \) 의 모든 조합을 오름차순으로 정렬하여 출력합니다.

단, 아래의 조건을 만족해야 합니다.

  1. 최소 한 개의 모음을 포함해야 합니다.
  2. 최소 두 개의 자음을 포함해야 합니다.

코드

import sys
from itertools import combinations

l, c = map(int, sys.stdin.readline().rstrip().split())
alps = list(sys.stdin.readline().rstrip().split())
vowels = ['a', 'e', 'i', 'o', 'u']
answer = []

candidates = list(combinations(alps, l))
for candidate in candidates:
	# 최소 한 개의 모음 & 최소 두 개의 자음
    if len(set(vowels) & set(candidate)) >= 1 and len(set(candidate) - set(vowels)) >= 2:
        answer.append(''.join(sorted(candidate)))
answer.sort()
for i in answer:
    print(i)