fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main
  5. {
  6. static class Node
  7. {
  8. int depth;
  9. String obj;
  10.  
  11. public Node(int depth, String obj)
  12. {
  13. this.depth = depth;
  14. this.obj = obj;
  15. }
  16. }
  17.  
  18. static TreeMap<String, ArrayList<Node>> map = new TreeMap<>();
  19. static int n;
  20.  
  21. public static void main(String[] args) throws IOException
  22. {
  23.  
  24. n = Integer.parseInt(br.readLine());
  25.  
  26. for (int i = 0; i < n; i++)
  27. {
  28. st = new StringTokenizer(br.readLine());
  29.  
  30. int idx = Integer.parseInt(st.nextToken());
  31. String root = st.nextToken();
  32. ArrayList<Node> nodes = map.getOrDefault(root, new ArrayList<>());
  33.  
  34. for (int j = 0; j < idx - 1; j++)
  35. {
  36. String obj = st.nextToken();
  37. nodes.add(new Node(j + 1, obj));
  38. // 각 노드가 추가될 때마다 출력
  39. printNode(root, new Node(j + 1, obj));
  40. }
  41.  
  42. map.put(root, nodes);
  43. }
  44. }
  45.  
  46. // 노드 출력을 위한 메소드
  47. static void printNode(String root, Node node) {
  48. StringBuilder sb = new StringBuilder();
  49. sb.append(root);
  50. for (int i = 0; i < node.depth; i++) {
  51. sb.append("--");
  52. }
  53. sb.append(node.obj);
  54. System.out.println(sb.toString());
  55. }
  56. }
  57.  
Success #stdin #stdout 0.08s 53100KB
stdin
4
2 KIWI BANANA
2 KIWI APPLE
2 APPLE APPLE
3 APPLE BANANA KIWI
stdout
KIWI--BANANA
KIWI--APPLE
APPLE--APPLE
APPLE--BANANA
APPLE----KIWI