|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.OutputStreamWriter; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.StringTokenizer; |
| 9 | +import java.util.TreeMap; |
| 10 | + |
| 11 | +public class BJ_14725_개미굴 { |
| 12 | + |
| 13 | + private static final String edge = "--"; |
| 14 | + |
| 15 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 16 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 17 | + private static final StringBuilder sb = new StringBuilder(); |
| 18 | + private static StringTokenizer st; |
| 19 | + |
| 20 | + private static int N; |
| 21 | + private static Trie trie; |
| 22 | + |
| 23 | + private static class Trie { |
| 24 | + TrieNode root; |
| 25 | + |
| 26 | + Trie() { |
| 27 | + root = new TrieNode(); |
| 28 | + } |
| 29 | + |
| 30 | + public void insert(String[] words, int length) { |
| 31 | + TrieNode node = root; |
| 32 | + |
| 33 | + for (int i = 0; i < length; i++) { |
| 34 | + node.children.putIfAbsent(words[i], new TrieNode()); |
| 35 | + node = node.children.get(words[i]); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public void print() { |
| 40 | + for (String food : root.children.keySet()) { |
| 41 | + _print(root.children.get(food), food, 0); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private void _print(TrieNode cur, String food, int level) { |
| 46 | + for (int i = 0; i < level; i++) { |
| 47 | + sb.append("--"); |
| 48 | + } |
| 49 | + sb.append(food).append("\n"); |
| 50 | + |
| 51 | + for (String nextFood : cur.children.keySet()) { |
| 52 | + _print(cur.children.get(nextFood), nextFood, level + 1); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private static class TrieNode { |
| 58 | + Map<String, TrieNode> children; |
| 59 | + |
| 60 | + TrieNode() { |
| 61 | + children = new TreeMap<>(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static void main(String[] args) throws IOException { |
| 66 | + init(); |
| 67 | + sol(); |
| 68 | + } |
| 69 | + |
| 70 | + private static void init() throws IOException { |
| 71 | + N = Integer.parseInt(br.readLine()); |
| 72 | + trie = new Trie(); |
| 73 | + |
| 74 | + for (int i = 0; i < N; i++) { |
| 75 | + st = new StringTokenizer(br.readLine()); |
| 76 | + |
| 77 | + int K = Integer.parseInt(st.nextToken()); |
| 78 | + String[] words = new String[K]; |
| 79 | + for (int j = 0; j < K; j++) { |
| 80 | + words[j] = st.nextToken(); |
| 81 | + } |
| 82 | + trie.insert(words, K); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private static void sol() throws IOException { |
| 87 | + trie.print(); |
| 88 | + |
| 89 | + bw.write(sb.toString()); |
| 90 | + bw.flush(); |
| 91 | + bw.close(); |
| 92 | + br.close(); |
| 93 | + } |
| 94 | + |
| 95 | +} |
| 96 | +``` |
0 commit comments