1759

이연희·2022년 6월 10일
0

Algorithm

목록 보기
8/9
import sys
input=sys.stdin.readline
L,C=map(int,input().split())
arr=sorted(list(map(str,input().split())))
vowel=['a','e','i','o','u']#모음
#최소 한개의 모음(vowel), 최소 두개의 자음(consonant)
s=[]
def dfs(depth,v_cnt,c_cnt):
    global s
    if depth==L:
        if v_cnt>=1 and c_cnt>=2:
            print(''.join(s))
        return
    for i in range(C):
        if depth==0 or s[depth-1]<arr[i]:
            s.append(arr[i])
            if  arr[i] in vowel:
                dfs(depth+1,v_cnt+1,c_cnt)
            else:
                dfs(depth+1,v_cnt,c_cnt+1)
            s.pop()

dfs(0,0,0)

모음 갯수(v_cnt)와 자음 갯수(c_cnt)의 정보를 재귀를 돌 때 파라메타로 넘겨준다.
depth의 길이가 조건 L과 같고, 모음이 1개 이상이고 자음이 2개 이상일 때 암호를 출력하고 리턴한다.
depth==0 or s[depth-1]<arr[i]: 가장 처음 들어왔을 때와 s에 저장된 마지막 원소가 집어넣을 원소값보다 작아야 한다.
예시를 들면 s=[b,c]일 때 집어넣을 원소(arr[i])가 a이면 c>a이므로 불가능하다. 반면 집어넣을 원소(arr[i])가 d이면 c<d이므로 가능하다.

profile
공부기록

0개의 댓글