Skip to content

Commit a660a36

Browse files
authored
Merge pull request #1583 from AlgorithmWithGod/zinnnn37
[20251204] BOJ / G3 / 디스크 트리 / 김민진
2 parents cf10104 + e5ee340 commit a660a36

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
```java
2+
import java.io.*;
3+
import java.util.Map;
4+
import java.util.TreeMap;
5+
6+
public class BJ_7432_디스크_트리 {
7+
8+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
private static final StringBuilder sb = new StringBuilder();
11+
12+
private static int N;
13+
private static Trie trie;
14+
private static String[] words;
15+
16+
private static class TrieNode {
17+
boolean isEnd;
18+
Map<String, TrieNode> children;
19+
20+
TrieNode() {
21+
children = new TreeMap<>();
22+
isEnd = false;
23+
}
24+
25+
}
26+
27+
private static class Trie {
28+
TrieNode root;
29+
30+
Trie() {
31+
root = new TrieNode();
32+
}
33+
34+
public void insert(String[] words) {
35+
TrieNode cur = root;
36+
37+
for (String word : words) {
38+
cur.children.putIfAbsent(word, new TrieNode());
39+
cur = cur.children.get(word);
40+
}
41+
cur.isEnd = true;
42+
}
43+
44+
public void print(int depth, TrieNode cur) {
45+
for (String word : cur.children.keySet()) {
46+
appendTabs(depth);
47+
sb.append(word).append("\n");
48+
print(depth + 1, cur.children.get(word));
49+
}
50+
}
51+
52+
private void appendTabs(int depth) {
53+
while (depth-- > 0) {
54+
sb.append(" ");
55+
}
56+
}
57+
58+
}
59+
60+
public static void main(String[] args) throws IOException {
61+
sol();
62+
}
63+
64+
private static void sol() throws IOException {
65+
N = Integer.parseInt(br.readLine());
66+
trie = new Trie();
67+
68+
while (N-- > 0) {
69+
words = br.readLine().split("\\\\");
70+
trie.insert(words);
71+
}
72+
trie.print(0, trie.root);
73+
bw.write(sb.toString());
74+
bw.flush();
75+
bw.close();
76+
br.close();
77+
}
78+
79+
}
80+
```

0 commit comments

Comments
 (0)