[Link] https://www.acmicpc.net/problem/14725
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader br;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
// StringBuilder sb = new StringBuilder();
int n = toi(br.readLine());
String[] line;
Trie trie = new Trie();
for(int i = 0; i < n; i++) {
trie.insert(getLine());
}
trie.dfs(0);
print(sb);
}
static public class Trie {
HashMap<String, Trie> list = new HashMap<>();
boolean sorted = false;
public void insert(String[] words) {
Trie t = this;
for(int i = 1; i < words.length; i++) {
if(!t.list.containsKey(words[i])) t.list.put(words[i], new Trie());
t = t.list.get(words[i]);
}
}
public void dfs(int depth) {
ArrayList<HashMap.Entry<String, Trie>> entries = new ArrayList<HashMap.Entry<String, Trie>>(this.list.entrySet());
Collections.sort(entries, (l, r) -> l.getKey().compareTo(r.getKey()));
for(HashMap.Entry<String, Trie> entry : entries) {
sb.append(new String("--").repeat(depth)).append(entry.getKey()).append("\n");
entry.getValue().dfs(depth + 1);
}
}
}
static int toi(String s) { return Integer.parseInt(s); }
static String[] getLine() throws IOException { return br.readLine().split(" "); }
static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
static <T> void print(T s) { System.out.print(s); }
static <T> void println(T s) { System.out.println(s); }
}