Codeforces Round #387 (Div. 2) E. Comments (dfs)
思路:按樹的深度進行 d f s dfs dfs即可,
#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;
}
原來之前寫的都是假的關閉流同步,
1.ios::sync_with_stdio(0) 這一步是取消
i
o
s
t
r
e
a
m
iostream
iostream和
s
t
d
i
o
stdio
stdio的同步,避免把輸出的東西先存入緩沖區再輸出來浪費不必要的時間,
2.
c
i
n
.
t
i
e
(
0
)
cin.tie(0)
cin.tie(0) 是取消
c
i
n
,
c
o
u
t
cin,cout
cin,cout的系結,進一步加快執行效率,
3.不能使用
e
n
d
l
endl
endl,這個東西會強制
f
l
u
s
h
b
u
f
f
e
r
flush\ buffer
flush buffer,即重繪緩沖區,而要使用
'\n' ,
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/180618.html
標籤:其他
