Skip to content

Commit b41a8b7

Browse files
committed
[String] baekjoon-5052
1 parent fadbbbd commit b41a8b7

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

โ€ŽREADME.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
| 07 | | [Baekjoon-10256 ๋Œ์—ฐ๋ณ€์ด](./src/String/P10256) | Aho-Corasick |
5151
| 08 | | [Baekjoon-15740 A+B - 9](./src/String/P15740) | |
5252
| 09 | | [Baekjoon-9093 ๋‹จ์–ด ๋’ค์ง‘๊ธฐ](./src/String/P9093) | |
53+
| 10 | | [Baekjoon-5052 ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก](./src/String/P5052) | |
5354

5455
## Brute Force
5556

โ€Žsrc/String/P5052/Main.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package String.P5052;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
static int T, N;
9+
10+
public static void main(String[] args) throws Exception {
11+
System.setIn(new FileInputStream("src/String/P5052/input.txt"));
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
14+
T = Integer.parseInt(br.readLine());
15+
while (T-- > 0) {
16+
N = Integer.parseInt(br.readLine());
17+
Trie t = new Trie();
18+
while (N-- > 0) t.insert(br.readLine());
19+
t.query();
20+
}
21+
}
22+
23+
static class Trie {
24+
Node root = new Node();
25+
26+
void insert(String s) {
27+
Node cur = root;
28+
for (int i = 0; i < s.length(); i++) {
29+
int c = s.charAt(i) - '0';
30+
if (!cur.hasChild(c)) cur.children[c] = new Node();
31+
cur = cur.children[c];
32+
}
33+
cur.isEnd = true;
34+
}
35+
36+
void query() {
37+
Queue<Node> q = new LinkedList<>();
38+
q.offer(root);
39+
while (!q.isEmpty()) {
40+
Node cur = q.poll();
41+
for (int i = 0; i < 10; i++) {
42+
if (cur.hasChild(i)) {
43+
if (cur.isEnd) {
44+
System.out.println("NO");
45+
return;
46+
}
47+
q.offer(cur.children[i]);
48+
}
49+
}
50+
}
51+
System.out.println("YES");
52+
}
53+
}
54+
55+
static class Node {
56+
Node[] children = new Node[10];
57+
boolean isEnd = false;
58+
59+
boolean hasChild(int i) { return children[i] != null; }
60+
}
61+
}

โ€Žsrc/String/P5052/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [baekjoon-5052] ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก
2+
3+
![image](https://user-images.githubusercontent.com/22045163/107534932-a68ea500-6c03-11eb-9487-3d0180734baa.png)
4+
5+
![image](https://user-images.githubusercontent.com/22045163/107535120-cd4cdb80-6c03-11eb-90a8-8db6f753bf8a.png)

โ€Žsrc/String/P5052/input.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2
2+
3
3+
911
4+
97625999
5+
91125426
6+
5
7+
113
8+
12340
9+
123440
10+
12345
11+
98346

0 commit comments

Comments
ย (0)