// UVa712 S-Trees
// Rujia Liu
// 题意:给一棵满二叉树,每一层代表一个01变量,取0时往左走,取1时往右走。给出所有叶子的值,以及一些查询(即每个变量的值),求最后到达的叶子的值
// 算法:结点从上到下编号为1, 2, 3, ...则左走就是乘以2,右走是乘以2加1。第一个叶子的编号是2^n
#include<iostream>
#include<string>
using namespace std; const int maxn = 10;
int n, v[maxn];//映射表
string leaves; int solve(const string& q) {
int u = 1;
for(int i = 0; i < n; i++) {
if(q[v[i]] == '0') u *= 2; else u = u*2+1;
}
return leaves[u-(1<<n)] - '0';
} int main() {
int kase = 0;
while(cin >> n && n) {
string s;
cout << "S-Tree #" << ++kase << ":\n";
for(int i = 0; i < n; i++) { cin >> s; v[i] = s[1] - '1'; }
int m;
cin >> leaves >> m;
while(m--) {
string q;
cin >> q;
cout << solve(q);
}
cout << "\n\n";
}
return 0;
}

我的解答:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
//0 left, 1 right const int N=10;
char leaves[1<<N];
//顺序映射表
int order[N];
int n; void solve(char* buf)
{
//最终叶节点的编号(从1开始)
int j=1;
for(int i=0;i<n;i++)
{
if(buf[order[i]]=='0')
j*=2;
else
j=j*2+1;
}
printf("%c", leaves[j-(1<<n)]);
} void getorder(char* s)
{
for(int i=0;i<n;i++)
{
order[i]=s[1+3*i]-'1';
}
} int main()
{
//freopen("./uva712.in", "r", stdin);
char buf[N*3+1];
int cnt=0;
while(scanf("%d", &n)==1 && n)
{
printf("S-Tree #%d:\n", ++cnt);
gets(buf);//read line gets(buf);//x1 x2 ...
getorder(buf); gets(leaves);
int m;
scanf("%d", &m); gets(buf);//read line
for(int i=0;i<m;i++)
{
gets(buf);
solve(buf);
}
printf("\n\n");
} return 0;
}

UVa712 S-Trees的更多相关文章

  1. [C#] C# 知识回顾 - 表达式树 Expression Trees

    C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...

  2. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  3. [LeetCode] Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  4. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  5. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  6. 2 Unique Binary Search Trees II_Leetcode

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  7. Finger Trees: A Simple General-purpose Data Structure

    http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...

  8. Christmas Trees, Promises和Event Emitters

    今天有同事问我下面这段代码是什么意思: var MyClass = function() { events.EventEmitter.call(this); // 这行是什么意思? }; util.i ...

  9. 【leetcode】Unique Binary Search Trees (#96)

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  10. LeetCode之Unique Binry Search Trees

    4月份很快就过半了,最近都在看WPF,有点落伍了...本来想写一点读书笔记的,还没想好要怎么写.所以为了能够达到每月一篇博客的目标,今天先说一个LeetCode上的面试题:Unique Binary ...

随机推荐

  1. UVA 550 Multiplying by Rotation (简单递推)

    题意:有些数字是可以这样的:abcd*k=dabc,例如179487 * 4 = 717948,仅仅将尾数7移动到前面,其他都不用改变位置及大小.这里会给出3个数字b.d.k,分别代表b进制.尾数.第 ...

  2. 【DFS+记忆搜索】NYOJ-10-Skiing

    [题目链接:NYOJ-10] skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑 ...

  3. z-index的妙用

    总是在纠结一个问题,当然我是前端初学者.这个问题就是,一个元素放在另一个元素里面,总希望它显示时,但是别撑开元素.这个时候一定要想到z-index. 例如今天写的一个浮动在导航栏下面的一个图片,我用的 ...

  4. Wireshark和TcpDump抓包分析心得

    Wireshark和 TcpDump抓包分析心得  1. Wireshark与tcpdump介绍 Wireshark是一个网络协议检测工具,支持Windows平台和Unix平台,我一般只在Window ...

  5. java中的getClass()函数

    Java反射学习 所谓反射,可以理解为在运行时期获取对象类型信息的操作.传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下,编程人员可以动态获取这些信息,从而编写更加具有可移植性的代 ...

  6. CSS计算样式的获取

    一般来说我们获取CSS的样式的时候会优先采用Elment.style.cssName 这种方法,这种方法类似于对象设置get,set属性获取,例如Elment.style.cssName是获取,Elm ...

  7. CentOS6.2编译安装codelite5.3

    这两天实验室要求在服务器上安装一个codelite,于是由我对服务器下手,上网看了下悲剧的发现codelite不支持在centos上直接安装,只能编译安装,经过一番折腾之后,成功的装上了,下面是我的安 ...

  8. C# 与 VC Dll 传输信息

    考虑: 使用string类型传送: 在VC Dll中解析字符: 使用 string 类型将解析的类型传送到C#程序中: 建立VC解析的函数,提高代码可重用性

  9. 【hbase】使用thrift with python 访问HBase

    HBase 版本: 0.98.6 thrift   版本: 0.9.0 使用 thrift client with python 连接 HBase 报错: Traceback (most recent ...

  10. IOS下arm64汇编疑问

    之前所有关于32位下的纯汇编.s代码,在编译arm64的时候,很多错误,不得已只能用C代码. 但是arm_neon.h内部类C接口的汇编,基本上没有问题.不敢完全保证,还有待确认.关于arm64位的汇 ...