[파이썬] 백준 1759: 암호만들기 (골드5)
1759번: 암호 만들기
첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.
www.acmicpc.net
문제
풀이
0. 방향성 생각
딱 보면 브루트포스/백트래킹
주어진 조건인 개수, 오름차순을 만족하는 문자열을 탐색하면 된다.
문자열의 수가 충분히 작아서 잘 조합해서 풀 수도 있는데 itertools에서 조합을 써서 풀기
1. 입력 받기
from itertools import combinations
l,c = map(int,input().split())
word = list(input().split())
temp = list(combinations(word,l))
사용되는 알파벳을 word에 받고 조합을 temp에 넣어준다.
2. 조건에 맞는 문자열 탐색
answer = []
for i in range(len(temp)):
count_v,count_c = 0,0
for j in range(len(temp[i])):
if temp[i][j] in ['a','e','i','o','u'] :
count_v += 1
else :
count_c += 1
if count_v >= 1 and count_c >= 2 :
a = list(temp[i])
a.sort()
answer.append(a)
정답을 넣어줄 answer 리스트를 만든다. 모음과 자음 조건을 세기 위해서 count_v, count_c를 만든다.
이후에 조합을 탐색한 후에 모음 자음 개수를 내부 for문에서 세준다.
개수 조건에 맞으면 단어를 정렬시키고 answer에 추가한다.
3. 출력
answer.sort()
for i in answer:
print(''.join(i))
정답도 오름차순으로 출력해야한다 했으니 answer를 출력하고 리스트로 된 문자열을 join을 사용해 합친 후 출력한다.
전체코드
from itertools import combinations
l,c = map(int,input().split())
word = list(input().split())
temp = list(combinations(word,l))
answer = []
for i in range(len(temp)):
count_v,count_c = 0,0
for j in range(len(temp[i])):
if temp[i][j] in ['a','e','i','o','u'] :
count_v += 1
else :
count_c += 1
if count_v >= 1 and count_c >= 2 :
a = list(temp[i])
a.sort()
answer.append(a)
answer.sort()
for i in answer:
print(''.join(i))
조건도 무난하고 골드치고는 쉬운듯
'Algorithm > etc' 카테고리의 다른 글
[파이썬] 백준 2559 : 수열 (실버3) (0) | 2023.05.24 |
---|---|
[파이썬] 백준 7869 : 두 원 (골드2) (0) | 2023.05.17 |
[파이썬] 백준 2166 : 다각형의 면적 (골드5) (0) | 2023.05.16 |
[파이썬] 백준 1411: 비슷한 단어 (실버2) (0) | 2023.05.08 |
[파이썬] 백준 - 15단계 약수,배수와 소수2 (0) | 2023.04.28 |
[파이썬] 백준 - 19단계 스택 (0) | 2023.04.22 |
[파이썬] 백준 - 실랜디 (0) | 2023.04.20 |
댓글