๐ฉ๐ปโ๐ป๋ฌธ์ ๋งํฌ
[๋ฐฑ์ค 1759] ์ํธ ๋ง๋ค๊ธฐ (Python)
โ๏ธIdea Sketch
2021-07-29
1. ํ์ด๋ฒ
from itertools import combinations
words = combinations(alpha, L)
# combinations(์กฐํฉํ ์์์ ๋ฐฐ์ด, ์กฐํฉํ ์์์ ๊ฐ์)
2. ์๋ก ๋ค๋ฅธ L๊ฐ์ ์ํ๋ฒณ์ ๋ฝ์ ํ(์กฐํฉ), ๋ฌธ์ ์กฐ๊ฑด์ ๋ง์กฑํ ๋ print
3. ๋์น ๋ถ๋ถ : ์ ๋ ฅ๊ฐ์ ๋ฐ์๋ง์ ์ ๋ ฌํ๊ธฐ
์ฌ๋ฐ๋ฅธ ์ฝ๋
L, C = map(int, input().split())
alpha = sorted(input().split()) #1, ์
๋ ฅ๊ฐ์ ๋ฐ์๋ง์ ์ ๋ ฌํ ๊ฒ
๋ฌธ์ ์ํฉ : ์๋์ ๊ฐ์ด printํ ๋๋ง ์ ๋ ฌํ๋ฉด, ๊ธฐ๋๊ฐ๊ณผ ์ถ๋ ฅ๊ฐ์ ์์๊ฐ ๋ฌ๋ผ์ง
alpha = input().split() #1
def is_password(arr):
print(''.join(sorted(arr))) #2
- ์ ๋ ฅ๊ฐ
4 6
a t c i s w
- ๊ธฐ๋๊ฐ
acis
acit
aciw
acst
acsw
actw
aist
aisw
aitw
astw
cist
cisw
citw
istw
- ์ถ๋ ฅ๊ฐ
acit
acst
actw
aist
aitw
astw
acis
aciw
acsw
aisw
cist
citw
istw # ๊ธฐ๋๊ฐ๊ณผ ๋ค๋ฆ
cisw # ๊ธฐ๋๊ฐ๊ณผ ๋ค๋ฆ
โ๏ธ์์ค์ฝ๋
2021-07-29
์กฐํฉ์ ๊ตฌํํ ํ์ด, 84ms ํต๊ณผ
L, C = map(int, input().split())
alpha = sorted(input().split()) #1
def is_password(arr):
cnt_vow = 0
for i in arr:
if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u':
cnt_vow += 1
if cnt_vow >= 1 and L - cnt_vow >= 2:
print(''.join(arr)) #2
def dfs(v, arr):
if len(arr) == L:
is_password(arr)
return
if v == C:
return
arr.append(alpha[v])
dfs(v+1, arr)
arr.pop()
dfs(v+1, arr)
dfs(0, [])
itertools์ ์ฌ์ฉํ ํ์ด, 72ms ํต๊ณผ
from itertools import combinations
L, C = map(int,input().split())
alpha = sorted(input().split())
words = combinations(alpha, L)
for word in words:
cnt_vow = 0
for i in word:
if i in "aeiou":
cnt_vow += 1
if cnt_vow >=1 and L - cnt_vow >=2:
print(''.join(word))