CF2.E
2 seconds
256 megabytes
standard input
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.
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;
输出每一层的字符串。
代码:
//这题好麻烦啊。先把字符串转换,每个单词有几个分支。然后暴力。
#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的更多相关文章
- 代码问题:【CF2】
[CF2/CFCF/HCF]: C Ma, JB Huang, X Yang, et al. Hierarchical convolutional features for visual tracki ...
- CF2.D
D. Santa Claus and a Palindrome time limit per test 2 seconds memory limit per test 256 megabytes in ...
- CF2.C
C. Vladik and fractions time limit per test 1 second memory limit per test 256 megabytes input stand ...
- CF2.D 并查集+背包
D. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...
- CF2.BC
B. Arpa's obvious problem and Mehrdad's terrible solution time limit per test 1 second memory limit ...
- CF2.C(二分贪心)
C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- *CF2.D(哥德巴赫猜想)
D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- cf2.25
T1 题意:判断给出的数中有多少不同的大于的数. content:傻逼题,5min手速 T2 题意:给出p.y,输出y~p+1中最大一个不是2-p的倍数的数. content:答案很简单,但是很难想到 ...
- BIRCH聚类算法原理
在K-Means聚类算法原理中,我们讲到了K-Means和Mini Batch K-Means的聚类原理.这里我们再来看看另外一种常见的聚类算法BIRCH.BIRCH算法比较适合于数据量大,类别数K也 ...
随机推荐
- C# winform 跨线程更改窗体控件的属性
当winform程序中新开一个线程,是无法改变主线程中窗体控件的属性的,否则运行时会报错. 若想在其他线程中控制主线程中的窗体控件,则必须利用BeginInvoke方法. 例如:添加一个名为textb ...
- Shell 脚本实现随机抽取班级学生
#/bin/bash function rand(){ min=$ max=$(($-$min+)) num=$(date +%s%N) echo $(($num%$max+$min)) } rnd= ...
- javase-->多线程--线程池
java的线程池理解 在面向对象编程中,对象创建和销毁是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源.在Java中更是如此,虚拟机将试图跟踪每一个对象,以便能够在对象销毁后进行垃圾回收. ...
- javaScript里的原型链
原型对象也是普通的对象,是对象一个自带隐式的__proto__属性,原型也有可能有自己的原型,如果一个原型对象的原型不为null的话,我们就称之为原型链.原型链是由一些用来继承和共享属性的对象组成的( ...
- PHP 使用分页方法修改多数据字段
这个标题听起来很别扭,需求是这样的.mysql中的customer表有5000条数据.现在要给customer表添加一个order_num 字段,客户每下单一次就update这个字段+1. 是的,新增 ...
- 关于Knockout的开始
最近看了汤姆大叔的Knockout,感到MVVM的神奇,也许这就是以后编程的方式,最直观的感觉就是层次更分明了. 绑定,模型转换,监控,在我来说,这算是编程上的一个转变,在以后我做不定期的更新一些MV ...
- android开发读书笔记
第九章心得: HAL ( Hardware Abstraction Layer,硬件抽象腔,〉是建立在Linux驱动之上的一套翻字库.这套程序 j率并不属于 Linux 内核, 而是属于 Linux ...
- RadioGroup 和 ViewPager 绑定 实现切换
package com.jereh.helloworld.activity.day12; import android.app.Activity; import android.os.Bundle; ...
- VS 编辑器扩展辅助工具
[工具]——[扩展和更新]——[联机]输入 C# outline 2015
- 中国剩余定理(Chinese Remainder Theorem)
我理解的中国剩余定理的含义是:给定一个数除以一系列互素的数${p_1}, \cdots ,{p_n}$的余数,那么这个数除以这组素数之积($N = {p_1} \times \cdots \tim ...