코딩관계론

[백준] 암호 만들기 본문

개발/알고리즘

[백준] 암호 만들기

개발자_티모 2024. 5. 7. 13:59
반응형

문제 이해하기

조교들이 새로운 보안 시스템을 설치하기로 했습니다. 이 시스템은 알파벳 소문자로 이루어진 암호를 사용하며, 최소 한 개의 모음과 최소 두 개의 자음이 포함되어야 합니다. 또한 알파벳은 증가하는 순서로 배열되어야 합니다. 주어진 C개의 문자로 가능성 있는 암호를 구하는 프로그램을 작성해야 합니다. 

 

문제 해결 방법 설명하기

1. 모든 알파벳을 조합해야 합니다.

아래의 코드를 사용하면 모든 조합을 구할 수 있습니다.

def possible_passwords(length, num_chars, characters):
    results = []
    combinations_list = list(combinations(characters, length))

 

2. 조건검사

만들어진 문자열 조합에서 모음과 자음의 개수를 구한 후 주어진 최소 조건에 만족되는지 검사합니다.

    for comb in combinations_list:
        count_vowels = 0
        count_consonants = 0
        for char in comb:
            if char in vowels:
                count_vowels += 1
            else:
                count_consonants += 1

코드

from itertools import combinations

def possible_passwords(length, num_chars, characters):
    results = []
    combinations_list = list(combinations(characters, length))
    
    vowels = ['a', 'e', 'i', 'o', 'u']
    
    for comb in combinations_list:
        count_vowels = 0
        count_consonants = 0
        for char in comb:
            if char in vowels:
                count_vowels += 1
            else:
                count_consonants += 1
                
        if count_vowels >= 1 and count_consonants >= 2:
            temp = "".join(comb)
            temp = sorted(temp)
            results.append(temp)
            
    return sorted(results)

if __name__ == "__main__":
    L, C = map(int, input().split())
    characters_list = input().split()
    passwords = possible_passwords(L, C, characters_list)
    for password in passwords:
        print("".join(password))

 

 

반응형