|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static int L, C; |
| 7 | + static char[] chars; |
| 8 | + static StringBuilder sb = new StringBuilder(); |
| 9 | + static Set<Character> vowels = Set.of('a', 'e', 'i', 'o', 'u'); |
| 10 | + |
| 11 | + public static void main(String[] args) throws IOException { |
| 12 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 14 | + |
| 15 | + L = Integer.parseInt(st.nextToken()); |
| 16 | + C = Integer.parseInt(st.nextToken()); |
| 17 | + |
| 18 | + chars = new char[C]; |
| 19 | + st = new StringTokenizer(br.readLine()); |
| 20 | + for (int i = 0; i < C; i++) { |
| 21 | + chars[i] = st.nextToken().charAt(0); |
| 22 | + } |
| 23 | + |
| 24 | + Arrays.sort(chars); // 정렬해두면 자연스럽게 사전순으로 탐색 |
| 25 | + |
| 26 | + backtrack(0, new char[L], 0); |
| 27 | + |
| 28 | + System.out.print(sb); |
| 29 | + } |
| 30 | + |
| 31 | + static void backtrack(int start, char[] password, int depth) { |
| 32 | + if (depth == L) { |
| 33 | + if (isValid(password)) { |
| 34 | + sb.append(password).append('\n'); |
| 35 | + } |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + for (int i = start; i < C; i++) { |
| 40 | + password[depth] = chars[i]; |
| 41 | + backtrack(i + 1, password, depth + 1); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + static boolean isValid(char[] password) { |
| 46 | + int vowelCount = 0; |
| 47 | + int consonantCount = 0; |
| 48 | + |
| 49 | + for (char c : password) { |
| 50 | + if (vowels.contains(c)) { |
| 51 | + vowelCount++; |
| 52 | + } else { |
| 53 | + consonantCount++; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return vowelCount >= 1 && consonantCount >= 2; |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
0 commit comments