[BOJ] 1759: 암호 만들기

이슬비·2023년 1월 20일
0

Algorithm

목록 보기
62/110
post-thumbnail

보이는가 ... 내 광기가 ...

1. 내 풀이

import sys
input = sys.stdin.readline

l, c = map(int, input().split())
char = list(map(str, input().split()))
char = sorted(char)

answer = []
result = []

def consonant(string):
    dict = {'a':0, 'b':1, 'c':1, 'd':1, 'e':0, 'f':1, 'g':1, 'h':1, 
            'i':0, 'j':1, 'k':1, 'l':1, 'm':1, 'n':1, 'o':0,
            'p':1, 'q':1, 'r':1, 's':1, 't':1, 'u':0, 'v':1, 
            'w':1, 'x':1, 'y':1, 'z':1}
    total = 0
    for i in range(len(string)):
        total += dict[string[i]]
    return total

def collection(string):
    dict = {'a':1, 'b':0, 'c':0, 'd':0, 'e':1, 'f':0, 'g':0, 'h':0, 
            'i':1, 'j':0, 'k':0, 'l':0, 'm':0, 'n':0, 'o':1,
            'p':0, 'q':0, 'r':0, 's':0, 't':0, 'u':1, 'v':0, 
            'w':0, 'x':0, 'y':0, 'z':0}
    total = 0
    for i in range(len(string)):
        total += dict[string[i]]
    return total

def dfs(start):
    if len(result) == l:
        if (consonant(result) >=2):
            if (collection(result) >=1):
                answer.append(''.join(result))
                return


    for i in range(start, len(char)):
        if char[i] not in result:
            result.append(char[i])
            dfs(i+1)
            result.pop()

dfs(0)
for i in answer:
    print(i)

풀이도 광기다 ...ㅎㅂㅎ
사실 광기보다는 이제 바보같음에 가까운 ...

글자를 오름차순으로 정렬하는 건 별로 안 어려웠는데 최소 라는 조건을 어떻게 따져줄지가 상당히 고민 되었다.

그 끝에 탄생한 Dictionary ...

지금보니 그렇게 똑똑한 풀이는 아님 ㅎㅎ

2. 다른 풀이

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)

https://pacific-ocean.tistory.com/454 내가 엄청 존경하는 분의 풀이이다.

확실히 깔끔 ... 하다.
dfs를 도는 부분은 크게 다르지 않은데, 자음&모음 조건을 확인 하는 부분이 if-else로 끝이 나서 ... ㅎㅎ 내가 너무 복잡하게 생각했던 것 같다.

3. 마치며

그래도 간만의 골드 문제를 내가 내 손으로 직접 풀어서 너무나도 뿌듯하다! ❤️🍀

profile
정말 알아?

0개의 댓글