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 ...
随机推荐
- 读书笔记-HBase in Action-第三部分应用-(2)GIS系统
本章介绍用HBase存储.高效查询地理位置信息. Geohash空间索引 考虑LBS应用中常见的两个问题:1)查找离某地近期的k个地点.2)查找某区域内地点. 假设要用HBase实现高效查找,首先要考 ...
- Linux下获取线程TID的方法
如何获取进程的PID(process ID)? 可以使用: #include <unistd.h> pid_t getpid(void); 通过查看头文件说明,可以得到更详细的信息: fi ...
- VTMagic的使用
// VTMagic的使用 // CFOrderViewController.m // qifuyuniOS //// /** * @author 李洪强, 16-08-30 10:08:50 ...
- 项目已经部署,tomcat已经启动,网址也没问题,却出现404错误
这个有可能是tomcat在初始化资源的时候发生了异常...判断tomcat是否发生异常就是看tomcat启动日志里有没有报错就行了. 另一种原因就是可能是修改了项目名称.因为web名称实际上是没有跟着 ...
- jsp_类的封装_集合的应用
一.需求分析 做一个jsp页面,动态显示信息表的内容. 1.做一个实体类:StudentInfo(包含4个字段) 2.如图模拟生成3条数据,本质上就是new StudentInfo 3个实例, 每一个 ...
- Vi/Vim查找替换使用方法【转】
原文地址:http://wzgyantai.blogbus.com/logs/28117977.html vi/vim 中可以使用 :s 命令来替换字符串.该命令有很多种不同细节使用方法,可以实现复杂 ...
- HDU1251 统计难题 【trie树】
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- lsblk df
df(1) - Linux manual page http://man7.org/linux/man-pages/man1/df.1.html report file system disk spa ...
- 1分钟看完 jQuery UI
jQuery UI简介 jQuery UI包含了许多维持状态的小部件(Widget),因此,它与典型的 jQuery 插件使用模式略有不同.所有的 jQuery UI 小部件(Widget)使用相同的 ...
- Lightoj 1023 - Discovering Permutations
1023 - Discovering Permutations PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory L ...