ํŒŒ์ด์ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[๋ฐฑ์ค€ 1759] ์•”ํ˜ธ ๋งŒ๋“ค๊ธฐ (Python)

์€์ง„ 2021. 8. 3. 14:07

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[๋ฐฑ์ค€ 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))