Advertisement
Alekss33

Untitled

Apr 26th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Collections;
  5. import java.util.Stack;
  6.  
  7. public class HDNL {
  8. public static void main(String[] args) throws IOException {
  9. BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11. int n = Integer.parseInt(scanner.readLine());
  12.  
  13. StringBuilder answer = new StringBuilder();
  14.  
  15.  
  16. String[] tags = new String[n];
  17. int[] indexes = new int[n];
  18. for (int i = 0; i < n; i++) {
  19. String input = scanner.readLine();
  20. StringBuilder strBuilder = new StringBuilder(input);
  21. int number = Integer.parseInt(strBuilder.deleteCharAt(0).toString());
  22.  
  23. tags[i] = input;
  24. indexes[i] = number;
  25. }
  26.  
  27. int curValue = indexes[0];
  28. int nestLvl = 0;
  29. String spaces = String.join("", Collections.nCopies(nestLvl, " "));
  30. answer.append(spaces).append("<").append(tags[0]).append(">").append("\n");
  31.  
  32. Stack<Tag> stack = new Stack<>();
  33. stack.push(new Tag(tags[0], indexes[0], nestLvl));
  34.  
  35. for (int i = 1; i < n; i++) {
  36. if (indexes[i] > curValue) {
  37. curValue = indexes[i];
  38. nestLvl++;
  39. spaces = String.join("", Collections.nCopies(nestLvl, " "));
  40. answer.append(spaces).append("<").append(tags[i]).append(">").append("\n");
  41.  
  42. stack.push(new Tag(tags[i], indexes[i], nestLvl));
  43. } else {
  44. curValue = indexes[i];
  45.  
  46. while (!stack.isEmpty() && curValue <= stack.peek().value) {
  47. Tag previousTag = stack.pop();
  48. spaces = String.join("", Collections.nCopies(previousTag.nestLvl, " "));
  49. answer.append(spaces).append("</").append(previousTag.tag).append(">").append("\n");
  50.  
  51.  
  52. if (!stack.isEmpty() && curValue < previousTag.value && curValue <= stack.peek().value) {
  53. nestLvl--;
  54. nestLvl = Math.max(nestLvl, 0);
  55. }
  56. }
  57. spaces = String.join("", Collections.nCopies(nestLvl, " "));
  58. answer.append(spaces).append("<").append(tags[i]).append(">").append("\n");
  59.  
  60. stack.push(new Tag(tags[i], indexes[i], nestLvl));
  61. }
  62.  
  63. }
  64.  
  65. while (!stack.isEmpty()) {
  66. Tag lastTag = stack.pop();
  67. spaces = String.join("", Collections.nCopies(lastTag.nestLvl, " "));
  68. answer.append(spaces).append("</").append(lastTag.tag).append(">").append("\n");
  69. }
  70.  
  71. System.out.println(answer);
  72.  
  73. }
  74.  
  75. public static class Tag {
  76. public String tag;
  77. public int value;
  78. public int nestLvl;
  79.  
  80. public Tag(String tag, int value, int nestLvl) {
  81. this.tag = tag;
  82. this.value = value;
  83. this.nestLvl = nestLvl;
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement