|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class Main { |
| 6 | + |
| 7 | + static StringBuilder sb = new StringBuilder(); |
| 8 | + public static void main(String[] args) throws Exception { |
| 9 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + |
| 11 | + int N = Integer.parseInt(br.readLine()); |
| 12 | + Trie trie = new Trie(); |
| 13 | + for(int i = 0; i < N; i++) { |
| 14 | + Trie in = trie; |
| 15 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 16 | + int K = Integer.parseInt(st.nextToken()); |
| 17 | + for(int j = 0; j < K; j++) { |
| 18 | + String s = st.nextToken(); |
| 19 | + |
| 20 | + // 이미 있을 때? |
| 21 | + Trie deep = in.children.get(s); |
| 22 | + |
| 23 | + if(deep == null) { |
| 24 | + in.children.put(s, new Trie()); |
| 25 | + deep = in.children.get(s); |
| 26 | + } |
| 27 | + |
| 28 | + in = deep; |
| 29 | + } |
| 30 | + } |
| 31 | + backTracking(trie, 0); |
| 32 | + System.out.println(sb); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + public static void backTracking(Trie trie, int depth) { |
| 37 | + |
| 38 | + String[] arr = trie.children.keySet().toArray(new String[0]); |
| 39 | + Arrays.sort(arr); |
| 40 | + for(String s : arr) { |
| 41 | + for(int i = 0; i < depth; i++) { |
| 42 | + sb.append("--"); |
| 43 | + } |
| 44 | + sb.append(s).append("\n"); |
| 45 | + backTracking(trie.children.get(s), depth + 1); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + static class Trie { |
| 50 | + HashMap<String, Trie> children = new HashMap<>(); |
| 51 | + } |
| 52 | + |
| 53 | + private static int read() throws Exception { |
| 54 | + int c; |
| 55 | + int n = 0; |
| 56 | + boolean negative = false; |
| 57 | + |
| 58 | + while ((c = System.in.read()) <= 32) { |
| 59 | + if (c == -1) return -1; |
| 60 | + } |
| 61 | + |
| 62 | + if (c == '-') { |
| 63 | + negative = true; |
| 64 | + c = System.in.read(); |
| 65 | + } |
| 66 | + |
| 67 | + do { |
| 68 | + n = n * 10 + (c - '0'); |
| 69 | + c = System.in.read(); |
| 70 | + } while (c > 32); |
| 71 | + |
| 72 | + return negative ? -n : n; |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | +} |
| 77 | +``` |
0 commit comments