728x90
SMALL
문제
https://www.acmicpc.net/problem/1759
1759번: 암호 만들기
첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.
www.acmicpc.net
풀이
백트래킹을 이용해서 해결했다.
import sys
def backtracking():
if len(password)==L:
cnt=0
for i in vowel: # 모음의 수 확인
cnt+=password.count(i)
if cnt>0 and L-cnt>1:
print("".join(map(str,password)))
return
for i in word:
if i not in password:
if len(password)==0 or password[-1]<i: #알파벳이 증가하는 순서대로 되었는지 확인
password.append(i)
backtracking()
password.pop()
L,C=map(int,sys.stdin.readline().split())
word=list(sys.stdin.readline().split())
word.sort()
password=[]
vowel='aeiou'
backtracking()
728x90
LIST
'코테공부 > python 백준' 카테고리의 다른 글
파이썬 백준 15649번: N과 M (1) (0) | 2024.04.16 |
---|---|
파이썬 백준 6603번: 로또 (0) | 2024.04.15 |
파이썬 백준 1074번: Z (0) | 2024.04.15 |
파이썬 백준 9663번: N-Queen (0) | 2024.04.15 |
파이썬 백준 17478번 : 재귀함수가 뭔가요? (0) | 2024.04.15 |