Huawei od unified exam B paper [comment conversion output] Java implementation

         All topics are available in five languages. C implementation directory , C++ implementation directory , Python implementation directory , Java implementation directory , JavaScript implementation directory

topic

On a blogging site, each blog post has comments. Each comment is a non-empty string of English letters.
Comments have a tree structure, each comment has a parent comment except the root comment.
When a comment is saved, the following format is used:

The first is the content of the comment;

Then there is the number of replies to the current comment.

And finally all child comments of the current comment. (sub-comments are stored nested using the same format)

For example:

The first comment is "hello,2,ok,0,bye,0", the second comment is "test,0", the third comment is "one,1,two,1,a,0" all comments is saved as "hello,2,ok,0,bye,0,test,0,one,1,two,1,a,0".
For comments in the above format, please print in another format:
first print the maximum depth of comment nesting.
Then print n lines, the i (1<=i<=n) line corresponds to the nesting level of comments (the nesting level of the root comment is 1) for the i line, the nesting level of the comments appear according to them are printed in sequence, separated by spaces.
 

Enter Description:
Line Comment. It is composed of English letters, numbers and English commas. It is guaranteed that each comment is a non-empty string composed of English characters. The number of each comment is an integer (consisting of at least one number). The length of the entire string does not exceed 106. Given The comment structure is guaranteed to be legal.
Output Description
Prints comments in the given format. For each level of nesting, comments should be printed in the order they appear in the input

Example 1
Input:
hello,2,ok,0,bye,0,test,0,one,1,two,1,a,0
Output:
3
hello test one
ok bye two

a
Note:
As shown in the figure in the title description, the maximum nesting level is 3. A comment at nesting level 1 is "hello test one", a comment at nesting level 2 is "ok bvetwo", and a comment at nesting level 3 is "a".

Example 2
Input:
A,5,A,0,a,0,A,0,a,0,A,0
Output:
2
A
A a A a A
Explanation:
As shown in the figure below, the maximum nesting level is 2, A comment at nesting level 1 is "A" and a comment at nesting level 2 is "A a A a A"
 

Example 3
input:
A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L, 0,M,2,N,0,0,1,P,0
Output:
4
AKM
BFHLNO
CDGIP
EJ
Description:
As shown in the figure below.
 

 

 

train of thought

Occupy the pit

Code

1

Require

Time limit: 1 second for C/C++, 2 seconds for other languages

Space limit: 262144K for C/C++, 524288K for other languages

64bit IO Format:%lld

语言限定:
C(clang11), C++(clang++11), Pascal(fpc 3.0.2), Java(javac 1.8), Python2(2.7.3), 
PHP(7.4.7), C#(mcs5.4), ObjC(gcc 5.4), Pythen3(3.9), JavaScript Node(12.18.2), JavaScript V8(6.0.0),
Sqlite(3.7.9), R(4.0.3), Go(1.14.4), Ruby(2.7.1), Swift(5.3), matlab(Octave 5.2), Pypy2(pypy2.7.13),
Pypy3(pypy3.6.1), Rust(1.44), Scala(2.11.12), Kotlin(1.4.10), Groovy(3.0.6), TypeScript(4.1.2), Mysql(8.0)

Guess you like

Origin blog.csdn.net/misayaaaaa/article/details/131110521