백준 1759 암호 만들기

김민영·2023년 1월 20일
0

알고리즘

목록 보기
84/125

과정

  • 백트래킹
  • 모음의 개수와 자음의 개수를 따로 세면서 재귀를 돌린다.
L, C = map(int, input().split())
lst= list(input().split())
lst.sort()

def main(level, now, idx, moum, saum):
    if level == L and moum >= 1 and saum >= 2:
        print(now)
        return
    for i in range(idx+1, C):
        now += lst[i]
        if lst[i] in ["a", "e", "u", "i", "o"]:
            moum += 1
            main(level + 1, now, i, moum, saum)
            moum -= 1
        else:
            saum += 1
            main(level + 1, now, i, moum, saum)
            saum -= 1
        now = now[:-1]
main(0, "", -1, 0, 0)
  • 자음과 모음 개수를 더한 후, 백트래킹을 하기 전에 다시 빼주지 않아서 자주 틀렸었다.
  • 기존 백트래킹을 사용할 때는 지금 코드의 level + 1 처럼 변수로 남기지 않아서 맞았던 것이었다.
profile
노션에 1차 정리합니당 - https://cream-efraasia-f3c.notion.site/4fb02c0dc82e48358e67c61b7ce8ab36?v=

0개의 댓글