A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed.
Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment.
When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format:
- at first, the text of the comment is written;
- after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments);
- after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm).
For example, if the comments look like:
![](https://i-blog.csdnimg.cn/blog_migrate/c70f74a59e19345081c07b106b13597f.png)
then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format:
- at first, print a integer d — the maximum depth of nesting comments;
- after that print d lines, the i-th of them corresponds to nesting level i;
- for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space.
The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas.
It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros.
The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid.
Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input.
hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0
3
hello test one
ok bye two
a b
a,5,A,0,a,0,A,0,a,0,A,0
2
a
A a A a A
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,O,1,P,0
4
A K M
B F H L N O
C D G I P
E J
The first example is explained in the statements.
题意:
看第一组样例,hello是顶点,有2个分支,hello的一个分支是ok,ok有0个分支,因此ok后面的bye是hello的一个分支,bye有0个分支,test是顶点,有0个分支,one是顶点,有1个分支,two是one的分支,two有2个分支a,b;
输出每一层的字符串。
代码:
1 //这题好麻烦啊。先把字符串转换,每个单词有几个分支。然后暴力。 2 #include<bits\stdc++.h> 3 using namespace std; 4 typedef long long ll; 5 int a[1000006],b[1000006]; 6 string s[1000006],s1[1000006]; 7 int main() 8 { 9 string ch; 10 cin>>ch; 11 int len=ch.size(); 12 int cnt=0,loca=-1,flag1=0; 13 for(int i=0;i<len;i++){ 14 if(ch[i]==','){ 15 cnt++; 16 if(cnt&1){ 17 flag1++; 18 for(int j=loca+1;j<i;j++) 19 s[flag1]+=ch[j]; 20 loca=i; 21 } 22 else{ 23 a[flag1]=0; 24 for(int j=loca+1;j<i;j++) 25 a[flag1]=10*a[flag1]+ch[j]-'0'; 26 loca=i; 27 } 28 } 29 } 30 int maxn=0,tem=0; 31 for(int i=1;i<=flag1;i++){ 32 int flag2=0; 33 for(int j=i-1;j>tem;j--){ 34 if(a[j]>0){ 35 a[j]--; 36 if(s1[b[j]+1].empty()) 37 s1[b[j]+1]+=s[i]; 38 else s1[b[j]+1]+=+" "+s[i]; 39 b[i]=b[j]+1; 40 flag2=1; 41 break; 42 } 43 } 44 if(!flag2){ 45 b[i]=1; 46 if(s1[1].empty()) 47 s1[1]+=s[i]; 48 else s1[1]+=" "+s[i]; 49 tem=i-1; 50 } 51 maxn=max(maxn,b[i]); 52 } 53 cout<<maxn<<endl; 54 if(maxn>0){ 55 for(int i=1;i<=maxn;i++){ 56 cout<<s1[i]<<endl; 57 } 58 } 59 return 0; 60 }