Codeforces Round #387 (Div. 2) E. Comments

E. Comments
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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).
All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma.

For example, if the comments look like:

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.
Input

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.

Output

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.

Examples
input
hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0
output
3
hello test one 
ok bye two 
a b 


input
a,5,A,0,a,0,A,0,a,0,A,0
output
2
a 
A a A a A 


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,O,1,P,0
output
4
A K M 
B F H L N O 
C D G I P 
E J 

Note

The first example is explained in the statements.


明显的dfs,预处理先把所有的字符串和数字放到相应的两个数组里。然后就从头开始dfs就可以了。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
vector<string>V[1000005];
int jie[100];
string in;
int len;
string s;
string str[1000005];
string nn[1000005];
int nnn[1000005];
int st;
int ans;
void dfs(int deep)
{
	if(deep>ans)
		ans=deep;
	int cir=nnn[st];
	//cout<<str[st]<<endl;
	V[deep].push_back(str[st]);
	st=st+1;
	for(int i=0;i<cir;i++)
	{
		dfs(deep+1);
	}
}
int main()
{
	jie[0]=1;
	for(int i=1;i<8;i++)
	{
		jie[i]=jie[i-1]*10;
	}
	while(cin>>in)
	{
		in+=',';
		len=0;
		s="";
		string num="";
		for(int i=0;i<in.length();i++)
		{
			if(in[i]==',')
			{
				if(num=="")
				{
					str[len++]=s;
					//cout<<s<<endl;
					s="";
				}
				else
				{
					nn[len-1]=num;
					//cout<<num<<endl;
					num="";
					
				}
				continue;
			}
			if(in[i]>='0'&&in[i]<='9')
			{
				num+=in[i];
			}
			else
			{
				s+=in[i];
			}
			
		}
		for(int i=0;i<len;i++)
		{
			int now=0;
			//cout<<nn[i]<<endl;
			for(int j=0;j<nn[i].length();j++)
			{
				now=now+(nn[i][j]-'0')*jie[nn[i].length()-j-1];
			}
			nnn[i]=now;
			//cout<<now<<endl;
		}
		//cout<<nnn[0]<<endl;
		//cout<<len<<endl;
		st=0;
		ans=-1;
		while(st<len)
		{
			dfs(1);
		}
		//dfs(1);
		cout<<ans<<endl;
		for(int i=1;i<=ans;i++)
		{
			for(int j=0;j<V[i].size();j++)
			{
				cout<<V[i][j]<<" ";
			}
			cout<<endl;
		}
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值