본문 바로가기
TIL - 프로그래밍/Python 알고리즘

[백준] 1759. 암호 만들기 - Python

by chaemj97 2022. 6. 9.
728x90

https://www.acmicpc.net/problem/1759

 

1759번: 암호 만들기

첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.

www.acmicpc.net


  • 코드1 - count
from itertools import combinations

# 암호의 길이 L, 암호로 사용했을 법함 문자의 종류 C
L, C = map(int,input().split())
word = list(input().split())

# 암호 문자 증가하는 순서로 배열
# 문자도 .sort()하면 오름차순으로 정렬!!
word.sort()

# L개씩 조합으로 뽑기
for i in combinations(word,L):
    # 모음의 개수 세기
    cnt = 0
    cnt += i.count('a')
    cnt += i.count('e')
    cnt += i.count('i')
    cnt += i.count('o')
    cnt += i.count('u')
    # 모음 1개 이상, 자음 2개 이상(모음의 개수가 최대 L-2)이면 출력
    if 1 <= cnt <= L-2:
        print(''.join(i))

 

  • 코드2 - set
from itertools import combinations
# 암호의 길이 L, 암호로 사용했을 법함 문자의 종류 C
L, C = map(int,input().split())
word = list(input().split())

# 암호 문자 증가하는 순서로 배열
# 문자도 .sort()하면 오름차순으로 정렬!!
word.sort()

# 모음
vowel = set('aeiou')
for i in combinations(word,L):
    # 차집합을 통해 자음 구하기
    consonant = set(i)-vowel
    # 자음 2개 이상, 모음 1개 이상(자음 최대 L-1)인가?
    if 2 <= len(consonant) <= L-1:
        print(''.join(i))
728x90
반응형

댓글