ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

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))
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€