E. Comments dfs模拟
http://codeforces.com/contest/747/problem/E
首先,把字符串变成这个样子。
hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0
hello 2 1
ok 0 2
bye 0 3
test 0 4
one 1 5
two 2 6
a 0 7
b 0 8
然后就可以按着每一位来dfs,第二个参数表示他有2个儿子,dfs的时候开个string ans[maxn]来保存每一层的字符串就可以了,
比赛的时候,傻傻逼逼地dfs构图,建好图后,又bfs,然后发现是头插法,又要reveser,很麻烦。不过幸好过了。
赛后补上这个方法。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#define root 1
const int maxn = 1e6 + ;
struct node {
string s;
int ti;
int id;
}a[maxn];
int lena;
char str[maxn];
string out[maxn];
string ans[maxn];
int up;
int mxdeep;
void dfs(int cur, int deep) {
up = max(up, cur);
mxdeep = max(mxdeep, deep);
ans[deep] += out[cur];
ans[deep] += " ";
for (int i = ; i <= a[cur].ti; ++i) {
dfs(up + , deep + );
}
}
void work() {
scanf("%s", str + );
int lenstr = strlen(str + );
int now = ;
string ts;
int di = ;
int to = ;
lenstr += ;
str[lenstr] = ',';
lena = ;
for (int i = ; i <= lenstr; ++i) {
if (now == ) {
if (str[i] == ',') {
now = ;
} else ts += str[i];
} else {
if (str[i] == ',') {
++lena;
a[lena].s = ts;
a[lena].ti = di;
a[lena].id = ++to;
out[to] = ts;
di = ;
ts.clear();
now = ;
} else di = di * + str[i] - '';
}
}
// for (int i = 1; i <= lena; ++i) {
//// printf("%d %d %d")
// cout << a[i].s << " " << a[i].ti << " " << a[i].id << endl;
// }
up = ;
while (up <= lena) {
dfs(up, );
up++;
}
printf("%d\n", mxdeep);
for (int i = ; i <= mxdeep; ++i) {
cout << ans[i] << endl;
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
E. Comments dfs模拟的更多相关文章
- Vijos P1114 FBI树【DFS模拟,二叉树入门】
		描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树1,它的结点类型也包括F结点,B结点和I结点三种 ... 
- HDU 5438 Ponds dfs模拟
		2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 # ... 
- POJ 3009 Curling 2.0(DFS + 模拟)
		题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ... 
- 牛客~~扫雷~~~DFS+模拟
		链接:https://www.nowcoder.com/acm/contest/118/F来源:牛客网 题目描述 <扫雷>是一款大众类的益智小游戏,于1992年发行.游戏目标是在最短的时间 ... 
- zzulioj--1089--make pair(dfs+模拟)
		1809: make pair Time Limit: 1 Sec Memory Limit: 128 MB Submit: 60 Solved: 44 SubmitStatusWeb Board ... 
- UVALive 6884 GREAT + SWERC = PORTO dfs模拟
		题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ... 
- DFS+模拟 ZOJ 3861 Valid Pattern Lock
		题目传送门 /* 题意:手机划屏解锁,一笔连通所有数字,输出所有可能的路径: DFS:全排列 + ok () 判断函数,去除一些不可能连通的点:) */ #include <cstdio> ... 
- Fabricate equation(dfs + 模拟)
		Fabricate equation Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Other ... 
- FZU 2108(dfs模拟,大数取余)
		K Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pr ... 
随机推荐
- Android  Activity与远程Service的通信学习总结
			当一个Service在androidManifest中被声明为 process=":remote", 或者是还有一个应用程序中的Service时,即为远程Service, 远程的意 ... 
- antd  离线 icon
			讲你下载下来的官方提供的字体库解压后所有文件复制到node-modules/antd/dist目录下 创建新的文件夹iconfont 在你项目生成的css入口文件对应的源码less文件开始添加如下两句 ... 
- [经典面试题]在O(1)时间删除链表结点
			[题目] 给定链表的头指针和一个结点指针.在O(1)时间删除该结点.链表结点的定义例如以下: struct ListNode { int value; struct ListNode* ... 
- 【solr专题之中的一个】Solr高速入门
			一.Solr学习相关资料 1.官方材料 (1)高速入门:http://lucene.apache.org/solr/4_9_0/tutorial.html.以自带的example项目高速介绍发Solr ... 
- mysql命令行爱好者必备工具mycli
			mycli MyCLI is a command line interface for MySQL, MariaDB, and Percona with auto-completion and syn ... 
- javaScript改变HTML
			改变HTML输出流: 在JavaScript中,document.write() 可用于直接向HTML输出流写内容 <!DOCTYPE html> <html> <bod ... 
- get the default proxy by Powershell
			https://stackoverflow.com/questions/571429/powershell-web-requests-and-proxies $proxyAddr = (get-ite ... 
- 安装tensorflow的最简单方法(Ubuntu 16.04 && CentOS)
			先说点题外话:在用anaconda安装很多次tensorflow失败之后,我放弃了,如果你遇到这样的问题:Traceback (most recent call last)-如果不是因为pip版本,就 ... 
- BZOJ_5343_[Ctsc2018]混合果汁_二分答案+主席树
			BZOJ_5343_[Ctsc2018]混合果汁_二分答案+主席树 题意:给出每个果汁的价格p,美味度d,最多能放的体积l.定义果汁混合后的美味度为果汁的美味度的最小值. m次询问,要求花费不大于g, ... 
- 第十六周 Leetcode 600. Non-negative Integers without Consecutive Ones(HARD) 计数dp
			Leetcode600 很简单的一道计数题 给定整数n 求不大于n的正整数中 二进制表示没有连续的1的数字个数 在dp过程中只要保证不出现连续1以及大于n的情况即可. 所以设计按位dp[i][j]表示 ... 
