【例题 6-7 UVA - 122 】Trees on the level
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
二叉树的话,直接用数组存就好了。
写个bfs记录一下答案。
【代码】
#include <bits/stdc++.h>
using namespace std;
const int N = 300;
string s;
int g[N+10][3],cnt;
vector <int> ans;
bool bfs(){
queue <int> dl;
dl.push(0);
while (!dl.empty()){
int x = dl.front();
dl.pop();
if (!g[x][2]) return false;
ans.push_back(g[x][2]);
for (int i = 0;i < 2;i++)
if (g[x][i])
dl.push(g[x][i]);
}
return true;
}
int main(){
// freopen("rush.txt","r",stdin);
while (cin >> s){
ans.clear();
memset(g,0,sizeof g);
cnt = 0;
bool ok = true;
while (!(s[0]=='(' && s[1]==')')){
int len = s.size();
bool root = false;
for (int i = 0;i < len;i++)
if (s[i]=='(' || s[i]==')' || s[i]==',') {
if (s[i]==',' && i+1<len && s[i+1]==')') root = true;
s[i]=' ';
}
stringstream ss(s);
int x;
ss >> x >> s;
if (root) s = "";
int now = 0;
for (int i = 0;i < (int) s.size();i++){
if (s[i]=='L'){
if (!g[now][0]) g[now][0] = ++cnt;
now = g[now][0];
}else{
if (!g[now][1]) g[now][1] = ++cnt;
now = g[now][1];
}
}
if (g[now][2]==0)
g[now][2] = x;
else{
ok = false;
}
cin >> s;
}
if (!ok || !bfs()){
puts("not complete");
}else{
for (int i = 0;i < (int) ans.size();i++){
printf("%d",ans[i]);
if (i==(int)ans.size()-1){
puts("");
}else putchar(' ');
}
}
}
return 0;
}
【例题 6-7 UVA - 122 】Trees on the level的更多相关文章
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
- 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 ...
- UVa 122 Trees on the level(二叉树层序遍历)
Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...
- UVa 122 Trees on the level
题目的意思: 输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余. 网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客 说一 ...
- UVa 122 Trees on the level(链式二叉树的建立和层次遍历)
题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...
- UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)
题意 :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...
- UVA - 122 Trees on the level (二叉树的层次遍历)
题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...
- 内存池技术(UVa 122 Tree on the level)
内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
随机推荐
- javap 命令 反编译代码
javap 命令 javap -c classname 一个好的分析class二进制文件的 链接 http://blog.csdn.net/pwlazy/article/details/7954169
- Rabin-Karp 算法
Rabin-Karp字符串查找算法 http://blog.chinaunix.net/uid-26548237-id-3968132.html
- Kinect 开发 —— 骨骼追踪(下)
Kinect 连线游戏 在纸上将一些列数字(用一个圆点表示)从小到大用线连起来.游戏逻辑很简单,只不过我们在这里要实现的是动动手将这些点连起来,而不是用笔或者鼠标. 在开始写代码之前,需要明确定义我们 ...
- java+spark-sql查询excel
Spark官网下载Spark Spark下载,版本随意,下载后解压放入bigdata下(目录可以更改) 下载Windows下Hadoop所需文件winutils.exe 同学们自己网上找找吧,这里就不 ...
- Java证书通信
一.概念介绍: 加密是将数据资料加密,使得非法用户即使取得加密过的资料,也无法获取正确的资料内容,所以数据加密可以保护数据,防止监听攻击.其重点在于数据的安全性.身份认证是用来判断某个身份的真实性 ...
- seq---生成随机数
seq命令用于产生从某个数到另外一个数之间的所有整数. 语法 seq [选项]... 尾数 seq [选项]... 首数 尾数 seq [选项]... 首数 增量 尾数 选项 -f, --format ...
- ManagementObjectSearcher 对象获取串口列表
首先,需引用using System.Management; 可先建个枚举类,如下 #region WIN32 API /// <summary> /// 枚举win32 api /// ...
- Myeclipse学习总结(2)——MyEclipse快捷键大全
1.ctrl+shift+R 打开资源 此组快捷键可以打开工程中任意一个文件,而本人只需按文件名或者mask名的字母顺序输入就会出现对应的文件或者在内容中某个关键字再按快捷键也可以的,例如:Custo ...
- 制作 wordpress 博客静态化到本地
wget 克隆 wordpress 博客镜像 wget -e robots=off -w 1 -xq -np -nH -pk -m -t 1 -P "./wordpress.org" ...
- 对 hiren bootcd 15.2 中的 XP 系统作了汉化, 同时支持中文输入法。提供下载
对 hiren bootcd 15.2 中的 XP 系统作了汉化, 同时支持中文输入法.提供下载 对该PE 中的 XP 系统作了汉化, 由于一个 中文字库 就要 10M 多:加之原系统过于精简,对中文 ...