|
| 1 | +""" |
| 2 | +BOJ #1759. ์ํธ ๋ง๋ค๊ธฐ (๊ณจ๋5) |
| 3 | +https://www.acmicpc.net/problem/1759 |
| 4 | +์ ํ: Graph, Backtracking |
| 5 | +""" |
| 6 | + |
| 7 | +""" |
| 8 | +ํ์ด1: ๋ฐฑํธ๋ํน |
| 9 | +""" |
| 10 | +import sys |
| 11 | +input = sys.stdin.readline |
| 12 | + |
| 13 | +L, C = map(int, input().split()) # L: ์ํธ ๊ธธ์ด, C: ๋ฌธ์ ์ข
๋ฅ |
| 14 | +chars = sorted(input().split()) # ์ฌ์ ์ ์ ๋ ฌ |
| 15 | +vowels = {'a', 'e', 'i', 'o', 'u'} |
| 16 | + |
| 17 | + |
| 18 | +def is_valid(word): |
| 19 | + # ์ต์ ํ ๊ฐ์ ๋ชจ์๊ณผ ์ต์ ๋ ๊ฐ์ ์์์ผ๋ก ๊ตฌ์ฑ๋์ด์๋์ง ํ์ธ |
| 20 | + vowel_cnt, consonant_cnt = 0, 0 # ๋ชจ์ ๊ฐ์, ์์ ๊ฐ์ |
| 21 | + for w in word: |
| 22 | + if w in vowels: |
| 23 | + vowel_cnt += 1 |
| 24 | + else: |
| 25 | + consonant_cnt += 1 |
| 26 | + |
| 27 | + return vowel_cnt >= 1 and consonant_cnt >= 2 |
| 28 | + |
| 29 | + |
| 30 | +def backtrack(word, start): |
| 31 | + if len(word) == L: # ์ข
๋ฃ ์กฐ๊ฑด |
| 32 | + if is_valid(word): |
| 33 | + print(''.join(word)) |
| 34 | + return |
| 35 | + |
| 36 | + for i in range(start, C): |
| 37 | + word.append(chars[i]) |
| 38 | + backtrack(word, i+1) |
| 39 | + word.pop() |
| 40 | + |
| 41 | +backtrack([], 0) |
| 42 | + |
| 43 | + |
| 44 | +""" |
| 45 | +ํ์ด2: ์กฐํฉ |
| 46 | +์ถ์ฒ: https://velog.io/@dlgosla/%EB%B0%B1%EC%A4%80-BOJ-%EC%95%94%ED%98%B8-%EB%A7%8C%EB%93%A4%EA%B8%B01759-python |
| 47 | +""" |
| 48 | +from itertools import combinations |
| 49 | + |
| 50 | +L, C = map(int, input().split()) |
| 51 | + |
| 52 | +alphabets = input().split() |
| 53 | + |
| 54 | +# ๊ธธ์ด๊ฐ L์ธ ๋ชจ๋ ์กฐํฉ, ์ฆ๊ฐํ๋ ์์๋ก ๋ฐฐ์ดํด์ผ๋๊ธฐ ๋๋ฌธ์ sort ํ comb |
| 55 | +alpha_combs = combinations(sorted(alphabets), L) |
| 56 | + |
| 57 | +answer = [] |
| 58 | + |
| 59 | +for alpha_comb in alpha_combs: # ๊ฐ๋ฅํ ์กฐํฉ ์ค์์ |
| 60 | + consonant_count = 0 |
| 61 | + vowel_count = 0 |
| 62 | + |
| 63 | + # ์์ ๋ชจ์ ๊ฐ์ ์ธ๊ธฐ |
| 64 | + for alpha in alpha_comb: |
| 65 | + if alpha in "aeiou": |
| 66 | + consonant_count += 1 |
| 67 | + else: |
| 68 | + vowel_count += 1 |
| 69 | + |
| 70 | + # ๋ชจ์์ด 1๊ฐ ์ด์, ์์์ด 2 ๊ฐ ์ด์์ด๋ฉด ์ถ๋ ฅ |
| 71 | + if consonant_count >= 1 and vowel_count >= 2: |
| 72 | + print("".join(alpha_comb)) |
| 73 | + |
| 74 | +""" |
| 75 | +ํ์ด3: DFS ์ฌ๊ท ๋ฐฉ์ |
| 76 | +์ถ์ฒ: https://velog.io/@dlgosla/%EB%B0%B1%EC%A4%80-BOJ-%EC%95%94%ED%98%B8-%EB%A7%8C%EB%93%A4%EA%B8%B01759-python |
| 77 | +""" |
| 78 | +L, C = map(int, input().split()) |
| 79 | + |
| 80 | +alphabets = sorted(input().split()) |
| 81 | + |
| 82 | + |
| 83 | +def dfs(idx, codes): |
| 84 | + if L == idx: |
| 85 | + vowel_count = 0 |
| 86 | + consonant_count = 0 |
| 87 | + |
| 88 | + # ์์ ๋ชจ์ ๊ฐ์ ์ธ๊ธฐ |
| 89 | + for code in codes: |
| 90 | + if code in "aeiou": |
| 91 | + consonant_count += 1 |
| 92 | + else: |
| 93 | + vowel_count += 1 |
| 94 | + |
| 95 | + # ์์ 2๊ฐ ์ด์, ๋ชจ์ ํ๊ฐ ์ด์์ด๋ฉด ์ํธ๊ฐ ๋ ์ ์์ผ๋ฏ๋ก ์ถ๋ ฅ |
| 96 | + if consonant_count >= 1 and vowel_count >= 2: |
| 97 | + print("".join(codes)) |
| 98 | + |
| 99 | + else: |
| 100 | + for i in range(idx, C): |
| 101 | + if codes and alphabets[i] <= codes[-1]: # ์ค๋ฆ์ฐจ์ ์๋๋ฉด ๋ฒ๋ฆผ |
| 102 | + continue |
| 103 | + dfs(idx + 1, codes + [alphabets[i]]) |
| 104 | + |
| 105 | + |
| 106 | +dfs(0, []) |
0 commit comments