Skip to content

Commit 0ab555f

Browse files
authored
[20251117] BOJ / G4 / 가르침 / 이준희
1 parent 0b9eaba commit 0ab555f

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
static int N, K;
8+
static String[] words;
9+
static boolean[] learned = new boolean[26];
10+
static int answer = 0;
11+
12+
static final char[] antic = {'a', 'n', 't', 'i', 'c'};
13+
14+
public static void main(String[] args) throws Exception {
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
18+
N = Integer.parseInt(st.nextToken());
19+
K = Integer.parseInt(st.nextToken());
20+
21+
words = new String[N];
22+
23+
for (int i = 0; i < N; i++) {
24+
words[i] = br.readLine().trim();
25+
}
26+
27+
if (K < 5) {
28+
System.out.println(0);
29+
return;
30+
}
31+
32+
if (K == 26) {
33+
System.out.println(N);
34+
return;
35+
}
36+
37+
for (char c : antic) {
38+
learned[c - 'a'] = true;
39+
}
40+
41+
dfs(0, 0);
42+
43+
System.out.println(answer);
44+
}
45+
46+
47+
static void dfs(int idx, int count) {
48+
if (count == K - 5) {
49+
answer = Math.max(answer, count());
50+
return;
51+
}
52+
53+
if (idx >= 26) return;
54+
55+
if (learned[idx]) {
56+
dfs(idx + 1, count);
57+
} else {
58+
learned[idx] = true;
59+
dfs(idx + 1, count + 1);
60+
61+
learned[idx] = false;
62+
dfs(idx + 1, count);
63+
}
64+
}
65+
66+
static int count() {
67+
int cnt = 0;
68+
69+
for (String word : words) {
70+
boolean canRead = true;
71+
72+
for (int i = 0; i < word.length(); i++) {
73+
char c = word.charAt(i);
74+
75+
if (!learned[c - 'a']) {
76+
canRead = false;
77+
break;
78+
}
79+
}
80+
81+
if (canRead) cnt++;
82+
}
83+
84+
return cnt;
85+
}
86+
}
87+
```

0 commit comments

Comments
 (0)