题意:给一棵满二叉树,叶子节点赋予权值,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. Lowest Common Ancestor of a Binary Search Tree 解答

    Question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes ...

  2. Search for a Range 解答

    Question Given a sorted array of integers, find the starting and ending position of a given target v ...

  3. Qt creator 搭配 valgrind 检测内存泄漏

    继上次重载operator new检测内存泄漏失败之后,妥协了.决定不管是否是准确指明哪一行代码出现内存泄漏,只要告诉我是否有泄漏就行了,这样就没有new替换的问题.在开发中,总是一个个小功能的开发. ...

  4. python之路-随笔 python处理excel文件

    小罗问我怎么从excel中读取数据,然后我百了一番,做下记录 以下代码来源于:http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html ...

  5. Android开源库集锦

    一.兼容类库 ActionBarSherlock : Action Bar是Android 3.0后才开始支持的,ActionBarSherlock是让Action Bar功能支持2.X后的所有平台, ...

  6. According to TLD or attribute directive in tag file, attribute value does not accept any expressions

    1.错误描写叙述 2014-7-13 17:27:21 org.apache.jasper.compiler.TldLocationsCache tldScanJar 信息: At least one ...

  7. Android进程间通信(IPC)机制Binder简要介绍和学习计划

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6618363 在Android系统中,每一个应用 ...

  8. JavaScript获取某年某月的最后一天

    JavaScript获取某年某月的最后一天 1.实现源代码 <!DOCTYPE html> <!-- To change this license header, choose Li ...

  9. arcengine 调用arctoolbox功能的举例 spatialJoin

    废话不多说,code是王道. 其中str1.str2两个参数是target路径.join路径 private void spatialJoin(Geoprocessor gp, string str1 ...

  10. 初识_IOS-简易计算器-问题总结

    作为一个新手,只好拿所有开发者都写的不想写又没有太大难度的计算器来下手咯.比较细一点,耐心哟. 我们都知道,计算器首要任务就是计算,那我们就直接看成A+B=C,来进行分析了.对A,B,C三个对象进行分 ...