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模拟的更多相关文章

  1. Vijos P1114 FBI树【DFS模拟,二叉树入门】

    描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树1,它的结点类型也包括F结点,B结点和I结点三种 ...

  2. HDU 5438 Ponds dfs模拟

    2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 # ...

  3. POJ 3009 Curling 2.0(DFS + 模拟)

    题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...

  4. 牛客~~扫雷~~~DFS+模拟

    链接:https://www.nowcoder.com/acm/contest/118/F来源:牛客网 题目描述 <扫雷>是一款大众类的益智小游戏,于1992年发行.游戏目标是在最短的时间 ...

  5. zzulioj--1089--make pair(dfs+模拟)

    1809: make pair Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 60  Solved: 44 SubmitStatusWeb Board ...

  6. UVALive 6884 GREAT + SWERC = PORTO dfs模拟

    题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  7. DFS+模拟 ZOJ 3861 Valid Pattern Lock

    题目传送门 /* 题意:手机划屏解锁,一笔连通所有数字,输出所有可能的路径: DFS:全排列 + ok () 判断函数,去除一些不可能连通的点:) */ #include <cstdio> ...

  8. Fabricate equation(dfs + 模拟)

    Fabricate equation Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Other ...

  9. FZU 2108(dfs模拟,大数取余)

     K Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Pr ...

随机推荐

  1. Linux OpenSSH后门的加入与防范

    引言:相对于Windows,Linux操作系统的password较难获取.只是非常多Linuxserver配置了OpenSSH服务.在获取root权限的情况下,能够通过改动或者更新OpenSSH代码等 ...

  2. HTML5_路径

    <!DOCTYPE html> <hmtl> <html  lang="zh-cn"> <head> <meta charse ...

  3. C项目实践--网络协议和套接字编程

    1.TCP/IP协议 TCP/IP协议是一组包括TCP协议和IP协议,UDP(User Datagram Protocol)协议,ICMP(Internet Control Message Proto ...

  4. python 线程 进程 标识

    s = '%s%s%s%s%s%s%s%s' % ( time.strftime('%Y%m%d %H:%M:%S', time.localtime(time.time())), ' os.getpp ...

  5. ios移动端浏览器点击事件失效的解决方案

    点击事件失效的原因可能是因为,你用了事件代理了, 比如这样 $(document).on("click",".fd",function(){ }) 这段代码在安 ...

  6. java的Date日期使用

    import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...

  7. VMware克隆虚拟机后网络配置

    修复克隆虚拟机文件 # vi /etc/udev/rules.d/70-persistent-net.rules # PCI device 0x8086:0x100f (e1000) SUBSYSTE ...

  8. OAuth学习总结

    1.为什么需要OAuth? 新浪微博就是你的家.偶尔你会想让一些人(第三方应用)去你的家里帮你做一些事,或取点东西.你可以复制一把钥匙(用户名和密码)给他们,但这里有三个问题: 1)别人拿了钥匙后可以 ...

  9. linux下libpcap抓包分析

    一.首先下载libpcap包http://www.tcpdump.org/#latest-release 然后安装,安装完成后进入安装根目录的tests文件夹,编译运行findalldevstest. ...

  10. 字节流与字符流简单操作(OutputStream、InputStream、Writer、Reader)

    操作流程 使用File类打开一个文件 通过字节流或者字符流的子类.指定输出的位置. 进行读/写操作 关闭输入/出 字节流与字符流 在java.io包中操作文件内容主要有两大类:字节流字符流.两大类分为 ...