Codeforces Round #387 (Div. 2) E. Comments (dfs)
发表时间:2020-10-18
发布人:葵宇科技
浏览次数:69
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' 。








