01204212/statuses

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
Course page: 01204212

You are given an information on comments are posted. You want to print out a report on that in a nicely form.

Input

  • First line: integer n (the number of comments)
  • The next n lines, each line specify a comment. Line i + 1 for 1<=i<=n specifies the i-th comment. In this line, there is an integer p (p < i) and a string s (consiting of a - z, A - Z, and an underline (_) whose length is at most 30). This comment replies to the p-th comment. If p=0, this comment replies to the status itself. The string s is the comment text.

Input example

7
0 hello_world
0 this_is_another_comment
1 first_reply
2 good_morning_thailand
1 second_one
1 third_one
5 when_it_is_ok

Output example

For this input, the expected report would be (see the description of the output at the end of this task description):

- 1 hello_world
  - 3 first_reply
  - 5 second_one
    - 7 when_it_is_ok
  - 6 third_one
- 2 this_is_another_comment
  - 4 good_morning_thailand

Output

The output contains n lines. Each line is a comment shown in the nested format (as in the example). Each line starts with a dash ("-") and is indented based on its comment level. (Comments of the status (i.e., those comments whose p=0) are level 0, comments that reply on these level 0 comments are level 1, comments that reply on level 1 comments are level 2, and so on.)

Test data: Download here

Sample outputs of the test data

n10.in

- 1 child_ask_time_eye_year_right
- 2 day_thing_high_year_that_you
  - 3 or_old_important_in_fact_be
  - 4 case_but_at_good_time_above
    - 9 work_I_good_work_their_last
  - 5 man_number_say_it_part_do
    - 7 little_few_you_under_as_try
      - 10 her_woman_try_that_case_this
- 6 by_thing_life_fact_little_old
  - 8 take_try_he_seem_different_in

n100.in (first 20 lines)

- 1 on_long_woman_ask_after_find
  - 4 fact_few_be_right_next_way
  - 95 about_her_way_it_in_to
- 2 take_case_but_first_child_big
  - 6 day_thing_way_try_give_do
    - 7 or_year_under_be_with_ask
      - 12 say_year_leave_life_up_her
        - 17 all_their_call_man_or_great
          - 31 give_call_woman_make_you_man
            - 38 know_under_work_man_or_for
              - 73 by_come_up_an_say_take
            - 49 and_by_her_their_above_have
              - 83 a_would_last_woman_feel_find
        - 22 of_next_you_a_same_last
          - 23 few_but_be_government_I_call
            - 33 their_work_early_high_way_for
        - 28 a_over_his_do_group_fact
          - 52 know_old_time_give_last_thing
          - 55 as_group_not_work_next_work
            - 87 work_eye_into_woman_it_man

Codes

Use these codes as your starting point.

Comment.java

public class Comment {

	private int id;
	private int parentId;
	private String msg;

	public Comment(int id, int parentId, String msg) {
		this.setId(id);
		this.setParentId(parentId);
		this.setMsg(msg);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getParentId() {
		return parentId;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}

Main.java

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

	public static void main(String [] args) throws Exception {
	    BufferedReader reader = new BufferedReader(
	               new InputStreamReader(System.in) );

		int n = Integer.parseInt(reader.readLine());
		Comment[] comments = readComments(n, reader); 
		
		// do your work here
	}

	private static Comment[] readComments(int n, BufferedReader reader) throws Exception {
		Comment[] comments = new Comment[n];
		
		for(int i=0; i<n; i++) {
			String[] items = reader.readLine().split(" ");
			Comment c = new Comment(i+1, Integer.parseInt(items[0]), items[1]);
			comments[i] = c;
		}
		return comments;
	}
}