Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HDNL Toy
- Steve found a new toy to play with. It's called HDNL (High Definition Native Language). He doesn't know what it is used for, he just finds it interesting. HDNL works by defining homeomorphic endofunctors mapping submanifolds of a Hilbert space. Sadly, when Steve is looking at HDNL, he isn't always able to imagine how all it would look in the end. Each line of HDNL consists of a letter and a number and opens a tag (like HTML tag). The letter is important, though Steve can't remember why. The number defines the level of nesting. Steve wants to see how he can nest all the tags such that the level of nesting of inner tags is bigger than that of outer tags. Your task is to write a program for Steve which shows nicely indented and closed HDNL tags.
- Input
- On the first line of input, a number N is read - the number of HDNL lines to follow
- Each of the next N lines will be a Latin letter glued to positive number
- Output
- There should be N * 2 lines
- Each output line should contain either an opening or a closing tag
- Use 1 space for indentation
- Constraints
- 1 <= N <= 100 000
- 1 <= level of nesting <= 1000
- Sample tests
- Input
- 4
- h1
- r5
- d2
- a0
- Output
- <h1>
- <r5>
- </r5>
- <d2>
- </d2>
- </h1>
- <a0>
- </a0>
- Input
- 9
- a1
- b2
- c3
- d3
- e2
- f3
- g2
- h1
- i2
- Output
- <a1>
- <b2>
- <c3>
- </c3>
- <d3>
- </d3>
- </b2>
- <e2>
- <f3>
- </f3>
- </e2>
- <g2>
- </g2>
- </a1>
- <h1>
- <i2>
- </i2>
- </h1>
- import java.util.*;
- public class Main {
- private static final StringBuilder result = new StringBuilder();
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int numOfLines = Integer.parseInt(scanner.nextLine());
- String[] tags;
- int[] nums;
- Stack<Tag> stack = new Stack<>();
- tags = new String[numOfLines];
- nums = new int[numOfLines];
- for (int i = 0; i < numOfLines; i++) {
- String input = scanner.nextLine();
- StringBuilder sb = new StringBuilder(input);
- int number = Integer.parseInt(sb.deleteCharAt(0).toString());
- tags[i] = input;
- nums[i] = number;
- }
- int curValue = nums[0];
- int nestLvl = 0;
- printOpenTag(nestLvl, tags[0]);
- stack.push(new Tag(tags[0], nums[0], nestLvl));
- for (int i = 1; i < numOfLines; i++) {
- if (nums[i] > curValue) {
- curValue = nums[i];
- nestLvl++;
- printOpenTag(nestLvl, tags[i]);
- stack.push(new Tag(tags[i], nums[i], nestLvl));
- } else {
- curValue = nums[i];
- while (!stack.isEmpty() && curValue <= stack.peek().value) {
- Tag newTag = stack.pop();
- printCloseTag(newTag.nestLvl, newTag.tag);
- if (curValue < newTag.value) {
- nestLvl--;
- nestLvl = Math.max(nestLvl, 0);
- }
- }
- printOpenTag(nestLvl, tags[i]);
- stack.push(new Tag(tags[i], nums[i], nestLvl));
- }
- }
- while (!stack.isEmpty()) {
- Tag newTag = stack.pop();
- printCloseTag(newTag.nestLvl, newTag.tag);
- }
- System.out.print(result);
- }
- private static void printCloseTag(int nestLvl, String tag) {
- String spaces = String.join("", Collections.nCopies(nestLvl, " "));
- result.append(spaces).append("<").append("/").append(tag).append(">").append("\n");
- }
- private static void printOpenTag(int nestLvl, String tag) {
- String spaces = String.join("", Collections.nCopies(nestLvl, " "));
- result.append(spaces).append("<").append(tag).append(">").append("\n");
- }
- public static class Tag {
- public String tag;
- public int value;
- public int nestLvl;
- public Tag(String tag, int value, int nestLvl) {
- this.tag = tag;
- this.value = value;
- this.nestLvl = nestLvl;
- }
- }
- }
- 2.
- import java.util.Scanner;
- import java.util.Stack;
- public class HDNL {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = Integer.parseInt(sc.nextLine());
- Stack<Integer> ranks = new Stack<>();
- Stack<String> closingTags = new Stack<>();
- int indent = 0;
- StringBuilder output = new StringBuilder();
- for (int i = 0; i < n; i++) {
- String inputLine = sc.nextLine();
- int rank = Integer.parseInt(inputLine.substring(1));
- while(!ranks.isEmpty() && ranks.peek()>=rank) {
- ranks.pop();
- String tag = closingTags.pop();
- indent--;
- printTag(output, indent, tag, "</%s>%n");
- }
- printTag(output, indent,inputLine, "<%s>%n");
- indent++;
- ranks.push(rank);
- closingTags.push(inputLine);
- }
- while (!closingTags.isEmpty()){
- String tag = closingTags.pop();
- indent --;
- printTag(output, indent,tag, "</%s>%n");
- }
- System.out.println(output);
- }
- private static void printTag(StringBuilder output, int indent, String tag, String format){
- for (int i = 0; i < indent; i++) {
- output.append(" ");
- }
- output.append(String.format(format,tag));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment