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 ...
随机推荐
- eclipse删除已经记录的用户名和密码
1.从windows > preference > Team > SVN #SVN Interface这个位置看看. 2.如果是用的JavaHL, 删除C:\Users\[YourU ...
- pcap文件格式及文件解析
第一部分:PCAP包文件格式 一 基本格式: 文件头 数据包头数据报数据包头数据报...... 二.文件头: 文件头结构体 sturct pcap_file_header { DWORD ...
- 在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
摘 自: http://blog.csdn.net/fxhflower/article/details/7276820 可使用以下代码: //Create our own namespaces for ...
- Can Live View boot up images acquired from 64bit OS evidence?
Some said Live View could only boot up images acquired from 32bit OS evidence. I have to say that it ...
- virtual box 中两个虚拟机 、宿主机 三机互通并且能上外网设置
virtual box 中两个虚拟机 .宿主机 三机互通并且能上外网设置 1:背景:因为需要学习linux,所以需要在虚拟机里装linux系统,测试要么宿主机与虚拟机linux网络实验测试:要么另一台 ...
- leetcode 8
string类型转换为int类型,需要考虑不同的转换情况. “ 04” 转换结果 4: “ 4 43” 转换结果 4: “a@12 ” 转换结果 0: “12a” ...
- GET POST 区别详解
Get是用来从服务器上获得数据,而Post是用来向服务器上传递数据. 2. Get将表单中数据的按照variable=value的形式,添加到action所指向的URL后面,并且两者使用“?”连接,而 ...
- RedHat安装VMwareTools出现解压压缩包时无法打开文件的现象
出现这种情况的原因是因为解压命令没有加—C参数,使用的命令为:tat -xvzf VMware Tools: 正确的解压命令应该是: tar -xvzf VMware Tools -C /opt,加上 ...
- 将Windows 7导航窗格中的收藏夹、库、家庭组、网络全部去掉
将Windows 7导航窗格中的收藏夹.库.家庭组.网络全部去掉,只剩下计算机.右键单击ShellFolder,选择权限->Administrators,勾选上“完全控制”,确定后双击右侧窗格中 ...
- HTML与XHTML的区别
为什么要使用XHTML? 我们认为万维网上的许多页面都包含着糟糕的 HTML 代码. 下面的 HTML 代码仍然可以工作得很好,即使它没有遵守 HTML 规则: <html> <he ...