File tree Expand file tree Collapse file tree 4 files changed +78
-0
lines changed Expand file tree Collapse file tree 4 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 50
50
| 07 | | [ Baekjoon-10256 ๋์ฐ๋ณ์ด] ( ./src/String/P10256 ) | Aho-Corasick |
51
51
| 08 | | [ Baekjoon-15740 A+B - 9] ( ./src/String/P15740 ) | |
52
52
| 09 | | [ Baekjoon-9093 ๋จ์ด ๋ค์ง๊ธฐ] ( ./src/String/P9093 ) | |
53
+ | 10 | | [ Baekjoon-5052 ์ ํ๋ฒํธ ๋ชฉ๋ก] ( ./src/String/P5052 ) | |
53
54
54
55
## Brute Force
55
56
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
1
+ 2
2
+ 3
3
+ 911
4
+ 97625999
5
+ 91125426
6
+ 5
7
+ 113
8
+ 12340
9
+ 123440
10
+ 12345
11
+ 98346
You canโt perform that action at this time.
0 commit comments