[BOJ] 1759

nerry·2022년 2월 1일
0

알고리즘

목록 보기
31/86

문제

me

import sys
import re
m = re.compile('[aeiou]')
input = sys.stdin.readline
L,C=map(int,input().split())
letters=list(map(str,input().split()))
letters.sort()

def dfs(cnt=0,pw=[],start=0):
    if cnt==L:
        temp=''.join(pw)
        len_m= len(m.findall(temp)) # 정규식을 통해 모음 개수 세기 
        if len_m>=1 and len(pw)-len_m>=2: # 자음 개수는 전체 예비번호 길이에서 모음 개수 빼서 구할 수 있다.
            print(temp)
        return
    else:
        for i in range(start,C):
            if letters[i] not in pw:
                pw.append(letters[i])
                dfs(cnt+1,pw,i+1)
                pw.pop()
dfs()

solution

import sys
input = sys.stdin.readline
def dfs(len, idx):
    if len == l:
        vo = 0
        co = 0
        for i in range(l):
            if arr[i] in 'aeiou': vo += 1
            else: co += 1
        if vo >= 1 and co >= 2:
            print(''.join(arr))
        return
    for i in range(idx, c):
        if check[i] == 0:
            arr.append(s[i])
            check[i] = 1
            dfs(len + 1, i + 1)
            check[i] = 0
            del arr[-1]
l, c = map(int, input().split())
check = [0 for i in range(c)]
arr = []
s = input().split()
s.sort()
dfs(0, 0)
profile
터벅터벅 개발(은좋은)자 로그

0개의 댓글