hdu 1622 Trees on the level
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622
小白书上的题。。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<queue>
using std::queue;
using std::vector;
const int Max_N = ;
struct Node {
int v, vis;
Node *ch[];
inline void set(int _v, Node *p) {
vis = , v = _v;
ch[] = ch[] = p;
}
};
struct BinTree {
int fail;
char buf[Max_N];
Node *tail, *root, stack[Max_N];
void init() {
fail = ;
tail = &stack[];
}
inline Node *newNode(int v = ) {
Node *p = tail++;
p->set(v, NULL);
return p;
}
inline void insert(const char *src, const int v) {
int n = strlen(src);
Node *x = root;
for (int i = ; i < n; i++) {
if (src[i] == 'L') {
if (!x->ch[]) x->ch[] = newNode();
x = x->ch[];
} else if (src[i] == 'R') {
if (!x->ch[]) x->ch[] = newNode();
x = x->ch[];
}
}
if (x->vis) fail = ;
x->v = v;
x->vis = ;
}
inline void bfs() {
vector<int> ans;
queue<Node *> que;
que.push(root);
while (!que.empty()) {
Node *u = que.front(); que.pop();
if (!u->vis) {
fail = ;
break;
}
ans.push_back(u->v);
if (u->ch[]) que.push(u->ch[]);
if (u->ch[]) que.push(u->ch[]);
}
if (fail) {
puts("not complete");
return;
}
int n = ans.size();
for (int i = ; i < n; i++) {
printf("%d%c", ans[i], i < n - ? ' ' : '\n');
}
}
inline int gogo() {
init();
int v = ;
root = newNode();
for (;;) {
if (scanf("%s", buf) != ) return ;
if (!strcmp(buf, "()")) break;
sscanf(&buf[], "%d", &v);
insert(strchr(buf, ',') + , v);
}
bfs();
return ;
}
}tree;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
while (tree.gogo());
return ;
}
hdu 1622 Trees on the level的更多相关文章
- hdu 1622 Trees on the level(二叉树的层次遍历)
题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...
- 【二叉树】hdu 1622 Trees on the level
[题意] 给定一棵树每个结点的权重和路径(路径用LR串表示),输出这棵树的层次遍历 [思路] 注意输入输出,sscanf用来格式化地截取需要的数据,strchr来在字符串中查找字符的位置 [Accep ...
- [ An Ac a Day ^_^ ] hdu 1662 Trees on the level 数据结构 二叉树
紫书上的原题 正好学数据结构拿出来做一下 不知道为什么bfs的队列一定要数组模拟…… 还可以练习一下sscanf…… #include<stdio.h> #include<iostr ...
- Trees on the level(指针法和非指针法构造二叉树)
Trees on the level Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- E - Trees on the level
Trees on the level Background Trees are fundamental in many branches of computer science. Current ...
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- uva 122 trees on the level——yhx
题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversa ...
随机推荐
- mysql中explain看性能
select distinct col_name from table where a=X and b=Y and date(time)='xx-xx-xx';执行时间 27.9772 秒 expla ...
- 深入探讨ES6生成器
如果对于ES6生成器不熟悉,请先阅读并运行下http://www.cnblogs.com/linda586586/p/4282359.html里面的代码.当你感觉掌握了基础之后,我们可以深入探讨一些细 ...
- [前端 3]纯Js制作俄罗斯方块游戏
导读:在别人文章里看到了,然后写了一遍.结果出错了,然后调出来了,然后理解了一下,加了点注释,有一些想法.忘了在 哪一篇上面看的了,就贴不出来链接地址.原谅.呃,真没自己的东西,权当练打字了吧.其实, ...
- Windows API学习---线程与内核对象的同步
前言 若干种内核对象,包括进程,线程和作业.可以将所有这些内核对象用于同步目的.对于线程同步来说,这些内核对象中的每种对象都可以说是处于已通知或未通知的状态之中.这种状态的切换是由Microsoft为 ...
- jqmobi 子菜单 高亮效果
在jqmobi 中经常用到一个 子菜单 单击某一个选项 希望能高亮:所以我是这样做的1.HTML的页面显示是这样的 <div class="order_subheader01" ...
- ios NSString常见的字符串操作 分割 查找
1.NSString *str = [[NSString alloc]init]; //简单粗暴,基本用不到 2.NSString *str = [[NSString alloc]initWi ...
- sorcketlog
正在运行的API有bug,不能var_dump进行调试,因为会影响client的调用. 将日志写到文件,查看也不方便,特别是带调用栈或大数据结构的文件日志,查看日志十分困难. 这时候用SocketLo ...
- dig out secrets beneath AirSig
My sister installed AirSig last week. She is so exciting about this new techknology and she won't st ...
- gdb调试工具使用方法分享
刚才看了一个CSDN上分享gdb调试工具使用的教程,讲得非常好,推荐到这里: http://blog.csdn.net/liigo/article/details/582231
- CSS3中颜色线性渐变实战
css3线性渐变可以设置3个参数值:方向.起始颜色.结束颜色.最简单的模式只需要定义起始颜色和结束颜色,起点.终点和方向默认自元素的顶部到底部.下面举例说明: CSS Code复制内容到剪贴板 .te ...