题意:给一棵满二叉树,叶子节点赋予权值,0或者1,对于每个查询输出叶子节点的权值,每个查询0代表往左走,1代表往右走,这题坑的地方是层的访问顺序,如第二组测试,由上到下依次是x3,x1,x2,假如给一个查询110,则从上到下的顺序是011,对应第3个叶子节点。二进制数转变成十进制数对应的叶子的权值,输出即可。

代码如下:

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=1000000007;
const double eps=0.00000001;
int deep; //最大深度
string L; //叶子对应的权值的字符串
int belong[10],leaf[1005],ans[1005]; //belong[]代表Xi对应第几层
void Get_Tree()
{
for(int i=0;i<deep;i++)
{
char S[4];
scanf("%s",S);
int id=S[1]-'0'-1; //分离编号
belong[id]=i; //记录
}
}
void Get_Leaf()
{
cin>>L; //输入叶子权值
}
int keep[10];
void query(int Case)
{
int M;
cin>>M;
string ret;
for(int i=1;i<=M;i++)
{
string command;
cin>>command;
int len=command.length();
for(int i=0;i<len;i++)
{
int a=command[i]-'0';
keep[belong[i]]=a; //保存下来
}
int id=0;
for(int i=0;i<len;i++) id=id*2+keep[i]; //得到十进制数
ret+=L[id];
}
printf("S-Tree #%d:\n",Case);
cout<<ret<<endl;
cout<<endl;
}
int main()
{
int kase=0;
while(cin>>deep)
{
if(!deep) break;
Get_Tree();
Get_Leaf();
query(++kase);
}
return 0;
}

UVA 712-S-Trees(满二叉树的简单查询)的更多相关文章

  1. UVa 712 S-Trees(二进制转换 二叉树叶子)

    题意: 给定一颗n层的二叉树的叶子, 然后给定每层走的方向, 0代表左边, 1代表右边, 求到达的是那一个叶子. 每层有一个编号, 然后n层的编号是打乱的, 但是给的顺序是从1到n. 分析: 先用一个 ...

  2. [LeetCode] 894. All Possible Full Binary Trees 所有可能的满二叉树

    A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list of al ...

  3. [Swift]LeetCode894. 所有可能的满二叉树 | All Possible Full Binary Trees

    A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list of al ...

  4. UVa 712 S树

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. UVA.679 Dropping Balls (二叉树 思维题)

    UVA.679 Dropping Balls (二叉树 思维题) 题意分析 给出深度为D的完全二叉树,按照以下规则,求第I个小球下落在那个叶子节点. 1. 默认所有节点的开关均处于关闭状态. 2. 若 ...

  6. 【algo&ds】【吐血整理】4.树和二叉树、完全二叉树、满二叉树、二叉查找树、平衡二叉树、堆、哈夫曼树、B树、字典树、红黑树、跳表、散列表

    本博客内容耗时4天整理,如果需要转载,请注明出处,谢谢. 1.树 1.1树的定义 在计算机科学中,树(英语:tree)是一种抽象数据类型(ADT)或是实作这种抽象数据类型的数据结构,用来模拟具有树状结 ...

  7. 【C++】满二叉树问题

    /* 给出一棵满二叉树的先序遍历,有两种节点:字母节点(A-Z,无重复)和空节点(#).要求这个树的中序遍历.输出中序遍历时不需要输出#. 满二叉树的层数n满足1<=n<=5. Sampl ...

  8. 二叉树的简单操作(Binary Tree)

    树形结构应该是贯穿整个数据结构的一个比较重要的一种结构,它的重要性不言而喻! 讲到树!一般都是讨论二叉树,而关于二叉树的定义以及概念这里不做陈诉,可自行搜索. 在C语言里面需要实现一个二叉树,我们需要 ...

  9. python实现满二叉树递归循环

    一.二叉树介绍点这片文章 二叉树及题目介绍 例题: 有一颗满二叉树,每个节点是一个开关,初始全是关闭的,小球从顶点落下, 小球每次经过开关就会把它的状态置反,这个开关为关时,小球左跑,为开时右跑.现在 ...

随机推荐

  1. asp.net mvc4中自定义404

    原文地址:http://www.chuchur.com/asp-net-mvc4-404/ 定义404 方法当然有很多种.不同的方法所展现的形式也不一样,用户所体验也不一样.以下提供2两种 方法一: ...

  2. Mysql update 错误

    今天在工作的时候发现自己update 一个表的某个字段超时,想了好久,首先想到的办法是,延长操作时间: mysql> set innodb_lock_wait_timeout=100 mysql ...

  3. hdu 5429 Geometric Progression(存个大数模板)

    Problem Description Determine whether a sequence is a Geometric progression or not. In mathematics, ...

  4. IOS 判断设备类型

    - (NSString*)deviceString { // 需要#import "sys/utsname.h" struct utsname systemInfo; uname( ...

  5. redis实现spring-data-redis整合

    java之redis篇(spring-data-redis整合)  博客链接网址:http://www.cnblogs.com/yjmyzz/tag/redis/ redis的知识:官网 1,利用sp ...

  6. LeetCode——Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  7. ERROR 1130: Host is not allowed to connect to this MySQL server

    解决远程连接mysql错误1130代码的方法 今天在用远程连接Mysql服务器的数据库,不管怎么弄都是连接不到,错误代码是1130,ERROR 1130: Host 192.168.2.159 is ...

  8. CSS基础知识笔记(四)

    元素分类 标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 常用的块状元素有: <div>.<p>.<h1>...<h6& ...

  9. jsp页面获取服务器时间

    Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MON ...

  10. CDZSC_2015寒假新人(2)——数学 A

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