01204212/statuses
รุ่นแก้ไขเมื่อ 23:24, 17 สิงหาคม 2559 โดย Jittat (คุย | มีส่วนร่วม)
- 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 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
Test data: Download here
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.)
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;
}
}