ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 1759 - 암호만들기
    카테고리 없음 2022. 1. 9. 02:12
    import itertools
    
    n,m = map(int, input().split())
    arr=list(sorted(input().split()))
    ans_list = []
    
    combination = list(itertools.combinations(arr, n))
    for text in combination:
        vowel = 0
        consonant = 0
        for word in text:
            if word in ['a', 'e', 'i', 'o', 'u']:
                vowel+=1
            else:
                consonant+=1
        if vowel>=1 and consonant>=2:
            password = ''.join(text)
            ans_list.append(password)
    ans_list = sorted(ans_list)
    
    for password in ans_list:
        print(password)

    우선 암호의 크기와 단어 수를 입력받는다.

    그 이후 사용할 알파벳 단어들을 정렬해서 입력받는다.

     

    itertools.combinations를 쓰면 조합의 경우의 수를 tuple 형태로 받을 수 있다. 

    그렇게 받은 tuple의 각 값을 반복해서 확인하여

    모음의 수가 1개 이상인지와 자음의 수가 2개 이상인지를 확인하여, 조건에 맞는 것만 ans_list에 append한다.

     

    이후 ans_list의 각 값을 출력한다.

Designed by Tistory.