Codeforces Round #387 (Div. 2) E. Comments (dfs) - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 技术分享 >

Codeforces Round #387 (Div. 2) E. Comments (dfs)

发表时间:2020-10-18

发布人:葵宇科技

浏览次数:47

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'

相关案例查看更多