Skip to content

Commit 1315e1b

Browse files
authored
Merge pull request #63 from AlgorithmWithGod/khj20006
[20250209] BOJ / 골드3 / 디스크 트리 / 권혁준
2 parents 955a5a0 + 6567f5d commit 1315e1b

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Node{
7+
String name;
8+
TreeMap<String, Node> subs;
9+
Node(String name){
10+
this.name = name;
11+
subs = new TreeMap<String, Node>();
12+
}
13+
}
14+
15+
class Trie{
16+
Node root;
17+
Trie(){
18+
root = new Node("ROOT");
19+
}
20+
}
21+
22+
class Main {
23+
24+
// IO field
25+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
26+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
27+
static StringTokenizer st;
28+
29+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
30+
static int nextInt() {return Integer.parseInt(st.nextToken());}
31+
static long nextLong() {return Long.parseLong(st.nextToken());}
32+
static void bwEnd() throws Exception {bw.flush();bw.close();}
33+
34+
// Additional field
35+
static Trie trie;
36+
static int N;
37+
38+
public static void main(String[] args) throws Exception {
39+
40+
ready();
41+
solve();
42+
43+
bwEnd();
44+
}
45+
46+
static void ready() throws Exception{
47+
48+
N = Integer.parseInt(br.readLine());
49+
trie = new Trie();
50+
51+
}
52+
53+
static void solve() throws Exception{
54+
55+
while(N-- > 0){
56+
String line = br.readLine();
57+
String[] dirs = line.split("\\\\");
58+
Node cur = trie.root;
59+
for(String dir : dirs){
60+
if(!cur.subs.containsKey(dir)) cur.subs.put(dir, new Node(dir));
61+
cur = cur.subs.get(dir);
62+
}
63+
64+
}
65+
66+
print(trie.root, -1);
67+
68+
}
69+
70+
static void print(Node cur, int dep) throws Exception{
71+
if(dep >= 0){
72+
for(int i=0;i<dep;i++) bw.write(" ");
73+
bw.write(cur.name + "\n");
74+
}
75+
for(String key : cur.subs.keySet()){
76+
Node val = cur.subs.get(key);
77+
print(val, dep+1);
78+
}
79+
}
80+
81+
}
82+
83+
```

0 commit comments

Comments
 (0)