백준 1759 암호만들기 (Python, Pypy3)

Joowan Park·2023년 8월 2일
0

코딩

목록 보기
17/28
import sys
import math
from itertools import permutations
from itertools import combinations

N,M = map(int,input().split())
symbol = list(input().split())
sym1 = []
sym2 = []
dic = ["a","e","i","o","u"]
for sym in symbol:
    if sym in dic:
        sym1.append(sym)
    else:
        sym2.append(sym)

pw = []
for comb in combinations(symbol,N):
    cnt1 = 0
    cnt2 = 0
    for x in comb:
        if x in sym1:
            cnt1 += 1
        else:
            continue
    for x in comb:
        if x in sym2:
            cnt2 += 1
        else:
            continue
    if 1 <= cnt1 and 2 <= cnt2:
        comb = list(comb)
        comb.sort()
        s = "".join(t for t in comb)
        pw.append(s)
    else:
        continue
    
pw = list(set(pw))
pw.sort()
for _ in pw:
    print(_)

주어진 단어에 대하여 모음 / 자음으로 분리 후
combination으로 먼저 N자리의 password를 추출해냅니다.

그리고 password에서 자음의 갯수와 모음의 갯수에 대한 조건을 만족하는지 확인 하는 작업 (for x in comb 부분)

그리고 조건을 만족하는 password에 대하여 이를 이어주면 되겠습니다.
(중복되는 password는 생략시켜주기)

profile
Complex Dynamics에서 탈출한 원숭이

0개의 댓글