Skip to content

Commit bbb8b5c

Browse files
authored
Merge pull request #1551 from AlgorithmWithGod/Seol-JY
[20251130] BOJ / G5 / 암호 만들기 / 설진영
2 parents 2001bbf + f42cbb6 commit bbb8b5c

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)