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.

题意:

看第一组样例,hello是顶点,有2个分支,hello的一个分支是ok,ok有0个分支,因此ok后面的bye是hello的一个分支,bye有0个分支,test是顶点,有0个分支,one是顶点,有1个分支,two是one的分支,two有2个分支a,b;

输出每一层的字符串。

代码:

 //这题好麻烦啊。先把字符串转换,每个单词有几个分支。然后暴力。
#include<bits\stdc++.h>
using namespace std;
typedef long long ll;
int a[],b[];
string s[],s1[];
int main()
{
string ch;
cin>>ch;
int len=ch.size();
int cnt=,loca=-,flag1=;
for(int i=;i<len;i++){
if(ch[i]==','){
cnt++;
if(cnt&){
flag1++;
for(int j=loca+;j<i;j++)
s[flag1]+=ch[j];
loca=i;
}
else{
a[flag1]=;
for(int j=loca+;j<i;j++)
a[flag1]=*a[flag1]+ch[j]-'';
loca=i;
}
}
}
int maxn=,tem=;
for(int i=;i<=flag1;i++){
int flag2=;
for(int j=i-;j>tem;j--){
if(a[j]>){
a[j]--;
if(s1[b[j]+].empty())
s1[b[j]+]+=s[i];
else s1[b[j]+]+=+" "+s[i];
b[i]=b[j]+;
flag2=;
break;
}
}
if(!flag2){
b[i]=;
if(s1[].empty())
s1[]+=s[i];
else s1[]+=" "+s[i];
tem=i-;
}
maxn=max(maxn,b[i]);
}
cout<<maxn<<endl;
if(maxn>){
for(int i=;i<=maxn;i++){
cout<<s1[i]<<endl;
}
}
return ;
}

CF2.E的更多相关文章

  1. 代码问题:【CF2】

    [CF2/CFCF/HCF]: C Ma, JB Huang, X Yang, et al. Hierarchical convolutional features for visual tracki ...

  2. CF2.D

    D. Santa Claus and a Palindrome time limit per test 2 seconds memory limit per test 256 megabytes in ...

  3. CF2.C

    C. Vladik and fractions time limit per test 1 second memory limit per test 256 megabytes input stand ...

  4. CF2.D 并查集+背包

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...

  5. CF2.BC

    B. Arpa's obvious problem and Mehrdad's terrible solution time limit per test 1 second memory limit ...

  6. CF2.C(二分贪心)

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. *CF2.D(哥德巴赫猜想)

    D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  8. cf2.25

    T1 题意:判断给出的数中有多少不同的大于的数. content:傻逼题,5min手速 T2 题意:给出p.y,输出y~p+1中最大一个不是2-p的倍数的数. content:答案很简单,但是很难想到 ...

  9. BIRCH聚类算法原理

    在K-Means聚类算法原理中,我们讲到了K-Means和Mini Batch K-Means的聚类原理.这里我们再来看看另外一种常见的聚类算法BIRCH.BIRCH算法比较适合于数据量大,类别数K也 ...

随机推荐

  1. GridView模版列中设置

    在GridView模版列中设置如下<asp:TemplateField HeaderText="删除">    <ItemTemplate>         ...

  2. winform中选择文件获取路径

    private void button1_Click(object sender, EventArgs e) { //此时弹出一个可以选择文件的窗体 OpenFileDialog fileDialog ...

  3. libsvm下的windows版本中的工具的使用

    下载的libsvm包里面已经为我们编译好了(windows).进入libsvm\windows,可以看到这几个exe文件: a.svm-toy.exe:图形界面,可以自己画点,产生数据等. b.svm ...

  4. php 封装 知识点

    类由众多对象抽象出来的对象由类实例化出来的 成员变量成员方法成员属性 访问修饰符public 公有的protected 受保护的private 私有的 构造函数1.写法特殊2.执行时间特殊 面向对象的 ...

  5. js 函数

    函数:封装了某一块功能 四要素: 1.返回类型 2.函数名 3.参数列表4.函数体强类型语言 返回类型 函数名 首字母大写 参数列表string(字符串) Show (int a){ 函数体 }弱类型 ...

  6. 预处理指令 #import

    vs class # import会包含这个类的所有信息,包括实体变量和方法, # @class只是告诉编译器,其后面声明的名称是类的名称,至于这些类是如何定义的,先不考虑.

  7. System系统类常用方法

    System 系统类: 主要用于获取系统的属性数据. System类常用的方法: arraycopy(Object src, int srcPos, Object dest, int destPos, ...

  8. 忘记Windows7登陆密码解决办法

    忘记 Windows7 的登陆密码,解决这个问题的思路就是替换 system32 下的 Magnify.exe . 可以从 WindowsPE 启动,到 C:\windows\system32 下. ...

  9. 二.TimesTen原理及应用场景

    声明:本文章转自麻袋爸爸 一,TimesTen应用场景 在谈论TimesTen内存数据库应用场景之前,我们先来介绍一下什么是内存数据库,及其工作原理吧.内存数据库,顾名思义就是将数据存放在内存中,并通 ...

  10. [Spring] AOP, Aspect实例解析

    最近要用到切面来统一处理日志记录,写了个小实例练了练手: 具体实现类: public interface PersonServer { public void save(String name); p ...