ํฐ์คํ ๋ฆฌ ๋ทฐ
๋ฌธ์
1759๋ฒ: ์ํธ ๋ง๋ค๊ธฐ
์ฒซ์งธ ์ค์ ๋ ์ ์ L, C๊ฐ ์ฃผ์ด์ง๋ค. (3 ≤ L ≤ C ≤ 15) ๋ค์ ์ค์๋ C๊ฐ์ ๋ฌธ์๋ค์ด ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋์ด ์ฃผ์ด์ง๋ค. ์ฃผ์ด์ง๋ ๋ฌธ์๋ค์ ์ํ๋ฒณ ์๋ฌธ์์ด๋ฉฐ, ์ค๋ณต๋๋ ๊ฒ์ ์๋ค.
www.acmicpc.net
๋ฌธ์ ํ์ด
์ด ๋ฌธ์ ๋ ํน์ ๋ฌธ์๋ค์ด ์ฃผ์ด์ง ๋, ํด๋น ๋ฌธ์์ ์กฐํฉ์ ๊ตฌํ๊ณ ๊ฐ ์กฐํฉ๋ค์ด 1๊ฐ ์ด์์ ๋ชจ์๊ณผ 2๊ฐ ์ด์์ ์์์ผ๋ก ๊ตฌ์ฑ๋๋ ๊ฒฝ์ฐ๋ฅผ ์ฐพ๋ ๋ฌธ์ ์ด๋ค. ์์ ๋ค๋ฃฌ 15650N๊ณผ M (2)์์ ์ค๋ณต์ ํ์ฉํ์ง ์๋ ์กฐํฉ์ DFS์ itertools๋ฅผ ํ์ฉํ์ฌ ๊ตฌํ ์ ์ด ์๋ค. ํด๋น ํ์ด๋ฅผ ์ดํดํ๊ณ ์๋ค๋ฉด ์ฝ๊ฒ ํ ์ ์๋ ๋ฌธ์ ์ด๋ค.
๊ธฐ์กด์ ์กฐํฉ ๋ฌธ์ ์ ๋ค๋ฅธ ์ ์ด ์๋ค๋ฉด, ์์๊ณผ ๋ชจ์์ ์๋ฅผ ํ๋จํ๋ ๋ถ๋ถ์ด ํ์ํ๋ค๋ ๊ฒ์ด๋ค.
-
depth๊ฐ L์ด๋ฉด, ์ฆ L๊ฐ ๋งํผ์ ๋ฌธ์๋ฅผ ์ ํํ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ ์กฐ๊ฑด์ ํ๋จํ์ฌ์ผ ํ๋ค.
- ๋ชจ์์ด 1๊ฐ ์ด์์ธ๊ฐ?
- ์์์ด 2๊ฐ ์ด์์ธ๊ฐ?
- ์์ ์กฐ๊ฑด์ ๋ชจ๋ ๋ง์กฑํ๋ ๊ฒฝ์ฐ๋ ์ ๋ต์ด๋ค.
์ฝ๋
DFS๋ฅผ ์ฌ์ฉํ ํ์ด
from sys import stdin
def dfs(idx, depth):
if depth == l:
v = 0
ret = ''
for i in check:
ret += char[i]
if char[i] in vowels:
v += 1
if v >= 1 and l - v >= 2:
cases.append(ret)
return
for i in range(idx, c):
if i not in check:
check[depth] = i
dfs(i, depth + 1)
check[depth] = -1
if __name__ == '__main__':
cases = []
l, c = map(int, stdin.readline().split())
char = sorted(stdin.readline().split())
vowels = ('a', 'e', 'i', 'o', 'u')
check = [-1] * l
dfs(0, 0)
for case in cases:
print(case)
itertools๋ฅผ ํ์ฉํ ํ์ด
from itertools import combinations
L, C = map(int, input().split())
strings = sorted(list(map(str, input().split())))
vowels = ('a', 'e', 'i', 'o', 'u')
for cases in combinations(strings, L):
cnt = 0
for case in cases:
if case in vowels:
cnt += 1
if 1 <= cnt <= L - 2:
print(''.join(cases))
'๐จโ๐ป ์ฝ๋ฉํ ์คํธ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค: 2529 ๋ถ๋ฑํธ (0) | 2020.07.18 |
---|---|
๋ฐฑ์ค: 14889 ์คํํธ์ ๋งํฌ (0) | 2020.07.17 |
๋ฐฑ์ค: 14501 ํด์ฌ (0) | 2020.07.16 |
๋ฐฑ์ค: 10971 ์ธํ์ ์ํ 2 (0) | 2020.07.16 |
๋ฐฑ์ค: 6603 ๋ก๋ (0) | 2020.07.15 |