UVa 122 Trees on the level
题目的意思:
输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余。
网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客
说一声:
之前看了网上好多代码,看的大段大段的,看不下去了,就准备自己写了,结果写出来也是大段大段的,其实中间有不少是被注释掉的测试代码,可忽略不计!
// 测试样例:udebug :链接
// 我整个代码最核心的整体思想就是把全部节点放到map中,
// 然后层级遍历到的就放到vector(V)中,并从map中删除(重点),最后如果map有剩余就认为not complete!
<span style="font-family:Courier New;font-size:14px;">#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
using namespace std;
struct s{
string path; //节点路径
int value; //节点值
};
vector <int> v; //存放可输出的树的节点
map <string, int> m; //存放树
map <string, int> ::iterator it,it1,it2;
queue <s> q;
//这个地方是重点,就是如何层级输遍历输出;
//在网上看了一下,层级遍历可以用队列,对于每个在队列里面的pop之后,把他的左儿子和右儿子加到队列里(如果有)
void levelordertravel(){
s temp;
string t,t1,x,y;
//如果连根都没有,直接返回了
it=m.find("");
if(it!=m.end()) q.push({it->first,it->second});
else return;
while(!q.empty()){
temp=q.front();
//cout<<temp<<endl;
v.push_back(temp.value);
t=temp.path;
t1=t;
m.erase(t);
q.pop();
//对于每个path,在map找加L和加R的
x=t.append("L");
it1=m.find(x);
y=t1.append("R");
it2=m.find(y);
if(it1!=m.end()){
q.push({it1->first,it1->second});
//cout<<"left son is in"<<endl;
}
if(it2!=m.end()){
q.push({it2->first,it2->second});
//cout<<"rigth son is in"<<endl;
}
}
}
bool input(){
string str;
while(cin>>str){
string path, value1;
int value, p, length;
if(str == "()") break;
length = str.length();
p = str.find(','); //,的位置
value1 = str.substr(1,p-1);
value = atoi(value1.c_str()); //这个地方把string转换成int
path = str.substr(p+1,length-1-(p+1));
it=m.find(path);
if(it!=m.end()) path="XXX";
m[path] = value;
}
return !cin.eof();
}
int main()
{
while(input()){ //这里的输入,没有循环输入的话就会wronganswer 就因为这个问题花费了很久时间
//int len=m.size();
//for(it=m.begin();it!=m.end();it++) cout<<it->first<<" "<<it->second<<endl;
//cout<<endl;
levelordertravel();
// for(int i=0;i<v.size();i++){
// cout<<v[i];
// if(i!=v.size()-1) cout<<" ";
// }
// cout<<endl;
if(m.size()!=0) cout<<"not complete"<<endl;
if(m.size()==0){
for(int i=0;i<v.size();i++){
cout<<v[i];
if(i!=v.size()-1) cout<<" ";
}
cout<<endl;
}
m.clear();
v.clear();
}
return 0;
}</span>
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(链式二叉树的建立和层次遍历)
题目链接: 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- ...
随机推荐
- WPF做12306验证码点击效果
一.效果 和12306是一样的,运行一张图上点击多个位置,横线以上和左边框还有有边框位置不允许点击,点击按钮输出坐标集合,也就是12306登陆的时候,需要向后台传递的参数. 二.实现思路 1.获取验证 ...
- 解决cookie跨域访问
一.前言 随着项目模块越来越多,很多模块现在都是独立部署.模块之间的交流有时可能会通过cookie来完成.比如说门户和应用,分别部署在不同的机器或者web容器中,假如用户登陆之后会在浏览器客户端写入c ...
- ,net core mvc 文件上传
工作用到文件上传的功能,在这个分享下 ~~ Controller: public class PictureController : Controller { private IHostingEnvi ...
- JVM类加载
JVM的类加载机制就是:JVM把描述类的class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被JVM直接使用的Java类型 ClassLoader JVM中的ClassLoade ...
- web服务器集群
概述 集群和分布式都是从集中式进化而来的.分布式和集群会相互合作的,同时的集群和分布式.在这里重点说说集群 集群是什么? 集群能提高单位时间内处理的任务数量,提升服务器性能 有多台服务器去处理任务,但 ...
- 【干货分享】流程DEMO-事务呈批表
流程名: 事务呈批表 业务描述: 办公采购.会议费用等事务的申请.流程发起时,会检查预算,如果预算不够,将不允许发起费用申请,如果预算够用,将发起流程,同时占用相应金额的预算,但撤销流程会释放相应金 ...
- Android中点击事件的实现方式
在之前博文中多次使用了点击事件的处理实现,有朋友就问了,发现了很多按钮的点击实现,但有很多博文中使用的实现方式有都不一样,到底是怎么回事.今天我们就汇总一下点击事件的实现方式. 点击事件的实现大致分为 ...
- BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2326 Solved: 1054[Submit][Status ...
- NYOJ 975
这道题一开始本着很朴素的想法就是先输入两头的数据,然后对每组的数据范围下测试中间的数据即可,但是是超时的.原因也很明显,比如计算1~1000的数据之后,假如下一组数据是1~1001,本来只需要多测试下 ...
- Jexus 服务器部署导航
说明:本索引只是方便本人查找,不涉及版权问题,所有博客,还是到元博客地址访问. <script async src="//pagead2.googlesyndication.com/p ...