// 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. highcharts 柱状图动态设置数据应用实例

    <div id="container" style="min-width:700px;height:400px"></div> #jav ...

  2. ubuntun安装ssh,并远程链接服务器操作

    SSH是一种以安全.加密方式连接远程主机或服务器的方法.SSH服务器接受从有SSH的客户机的连接,允许操作者象在本地一样地登录系统.你可以用SSH从远程运行shell和X程序. (1)安装SSH服务器 ...

  3. 是时候学习Android分屏开发了

    今年Google发布了Android N,Android N新增了不少功能,最受关注的自然就是分屏了. 这一功能对国内的很多手机用户并不陌生,其实很多第三方系统早已经实现了这一功能,如EMUI,Fly ...

  4. Iwpriv工作流程及常用命令使用

    iwpriv工具通过ioctl动态获取相应无线网卡驱动的private_args所有扩展参数 iwpriv是处理下面的wlan_private_args的所有扩展命令,iwpriv的实现上,是这样的, ...

  5. linux 命令——文件管理 cat

    一.介绍 cat 是一个文本文件查看和连接工具.从第一个字节开始正向查看文件的内容. 主要有三大功能: 1.一次显示整个文件.$ cat   filename ~,y6;e2.从键盘创建一个文件.$ ...

  6. ios游戏开发--cocos2d学习(1)

    学习cocos2d需要一定的编程基础,最好了解objective-c的语法.至于下载和安装的过程网上有很多,这里不多介绍,直接进入项目的学习. 创建一个cocos2d项目,直接运行,效果如图: 左下角 ...

  7. SVM:从理论到OpenCV实践

    (转载请注明出处:http://blog.csdn.net/zhazhiqiang/ 未经允许请勿用于商业用途)   一.理论 参考网友的博客: (1)[理论]支持向量机1: Maximum Marg ...

  8. PHP图像操作:3D图、缩放、旋转、裁剪、添加水印(三)

    来源:http://www.ido321.com/887.html 5.PHP对图像的旋转 1: <div> 2: <h4>旋转之前</h4> 3: <img ...

  9. 《一步一步写嵌入式操作系统》读书笔记1—Skyeye介绍、安装和HelloWorld

    2013-11-14 最近在看<一步一步写嵌入式操作系统>,感觉此书甚好,许多地方讲得很清楚.可操作性强,计划边读边实践边写笔记,希望能够逐步熟悉嵌入式操作系统底层的东西,最终剪裁出一套实 ...

  10. web.py实现jsonp

    浏览器端请求 $.getJSON("/currenttime?callback=?", function (json){ $("#time").html(jso ...