UVa712 S-Trees
// 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的更多相关文章
- [C#] C# 知识回顾 - 表达式树 Expression Trees
C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...
- hdu2848 Visible Trees (容斥原理)
题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 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 ...
- 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 ...
- Christmas Trees, Promises和Event Emitters
今天有同事问我下面这段代码是什么意思: var MyClass = function() { events.EventEmitter.call(this); // 这行是什么意思? }; util.i ...
- 【leetcode】Unique Binary Search Trees (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LeetCode之Unique Binry Search Trees
4月份很快就过半了,最近都在看WPF,有点落伍了...本来想写一点读书笔记的,还没想好要怎么写.所以为了能够达到每月一篇博客的目标,今天先说一个LeetCode上的面试题:Unique Binary ...
随机推荐
- Mysqlbackup 备份详解(mysql官方备份工具)
A.1全库备份. 命令: mysqlbackup --defaults-file=/home/mysql-server/mysql3/my.cnf --user=root --password=ro ...
- 图片的android:src 及android:background共存
---恢复内容开始--- 需求:给ImageView添加背景色 效果: 实现分析: 1.目录结构: 代码实现: 1.activity_main.xml <merge xmlns:android= ...
- 转载:fstream和ifstream详细用法
文件 I/O 在C++中比烤蛋糕简单多了.在这篇文章里,我会详细解释ASCII和二进制文件的输入输出的每个细节,值得注意的是,所有这些都是用C++完成的. 一.ASCII 输出 为了使用下面的方法, ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 list_should_have_no_selections(self, locator)
def list_should_have_no_selections(self, locator): """Verifies select list identified ...
- 基于51,人体红外感应和RC522的门禁系统
总结一下最近学的东西,这两天学的东西,rfid门卡系统终于弄出来来了,这个程序算现在写过的比较满意的程序,大家可以参考参考 主函数: #include<reg52.h> #include& ...
- pci 记录
用linux 下的sysfs可以方便的查看pci设备的配置和资源. 所有的pci设备在/sys/bus/pci/device 下面看到 pci配置空间对应的是设备对应的目录下的config文件,是二进 ...
- 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point
// 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point // 思路:直接暴力绝对T // 先确定x范围,每个x范围内,离圆心最远的点一定是y轴两端的点.枚举x的范围,再 ...
- (转载)OC学习篇之---类的定义
之前已经介绍了OC中的一个程序HelloWorld,今天我们继续学习OC中类的相关知识. OC和C的最大区别就是具有了面向对象的功能,那么说到面向对象,就不得不说类这个概念了,如果学过Java的话,那 ...
- 【Spark学习】Apache Spark监控与测量
Spark版本:1.1.1 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4137952.html
- Chapter 1 初探Caffe
首先下载windows下源码: Microsoft 官方:GitHub - Microsoft/caffe: Caffe on both Linux and Windows 官方源码使用Visual ...