Codeforces Round #387 (Div. 2) E. Comments (dfs)
Idea: dfs dfs according to the depth of the treed f s is fine.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
#define mst(a,b) memset(a,b,sizeof a)
#define lx x<<1
#define rx x<<1|1
#define reg register
#define PII pair<int,int>
#define fi first
#define se second
#define pb push_back
#define il inline
#define ios ios::sync_with_stdio(0),cin.tie(0)
int mx;
string d[N];
bool dfs(int dep){
string s;
getline(cin,s,',');
if(s=="") return false;
d[dep]+=s+" ";
getline(cin,s,',');
int n=stoi(s);
while(n--) dfs(dep+1);
mx=max(mx,dep);
return true;
}
int main(){
ios;
while(dfs(1));
cout<<mx<<'\n';
for(int i=1;i<=mx;i++) cout<<d[i]<<'\n';
return 0;
}
It turns out that what I wrote before was all false close stream synchronization.
1.ios::sync_with_stdio(0) This step is to cancel iostream iostreamiostream和 s t d i o stdio S T D I O is synchronized to avoid things into the buffer output lose out unnecessary waste of time.
2.cin. tie (0) cin.tie(0)c i n . t i e ( 0 ) is to cancelcin, cout cin, coutc i n ,The binding of c o u t further accelerates the execution efficiency.
3.Cannot use endl endle n d l , this thing will forceflush buffer flush\ bufferf l u s h b u f f e r , that is, to refresh the buffer, but to be used
'\n'
.