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 ...
随机推荐
- 查看ORACLE执行计划的几种常用方法
SQL的执行计划实际代表了目标SQL在Oracle数据库内部的具体执行步骤,作为调优,只有知道了优化器选择的执行计划是否为当前情形下最优的执行计划,才能够知道下一步往什么方向. 执行计划的定义:执行目 ...
- 【jQuery】jQuery筛选器规则
转载自:http://blog.csdn.net/lijinwei112/article/details/6938134 筛选器中加入变量 var ac = "select_" + ...
- 【Mysql】安装 mysql-5.7.5 指南
因为同学需要安装mysql,安装过程,一路百度,在这里记录一下步奏.以后还会用到. 1.mysql-5.7.5-m15-winx64.zip下载 官方网站下载地址:http://cdn.mysql.c ...
- 【字符串处理】HDOJ-1020-Encoding
[题目链接:HDOJ-1020] 相邻字符,两两比较. #include<cstdio> #include<cstring> ; char sr[MAXN]; int main ...
- 去除 waring Method 'CreateNew' hides virtual method of base type 'TCustomForm'
最近整理前人的代码,有好多的hint和waring, 其中整理到Method 'CreateNew' hides virtual method of base type 'TCustomForm', ...
- hdu 5335 Walk Out(bfs+斜行递推) 2015 Multi-University Training Contest 4
题意—— 一个n*m的地图,从左上角走到右下角. 这个地图是一个01串,要求我们行走的路径形成的01串最小. 注意,串中最左端的0全部可以忽略,除非是一个0串,此时输出0. 例: 3 3 001 11 ...
- Tkinter教程之Canvas篇(2)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811888 '''Tkinter教程之Canvas篇(2)''''''9.创建item的tag ...
- 纯css3鼠标经过图片显示描述特效
http://***/Article/5582 今天给大家带来的是用css3技术实现鼠标经过图片,显示图片描述的动画效果.鼠标经过图片时,图片动画缩小并渐变显示描述.我们一起看看效果图: 在线预览 ...
- File-nodejs
文件系统模块是一个简单包装的标准 POSIX 文件 I/O 操作方法集.您可以通过调用require('fs')来获取该模块.文件系统模块中的所有方法均有异步和同步版本. 文件系统模块中的异步方法需要 ...
- hdfs 数据块重分布 sbin/start-balancer.sh -threshold
数据块重分布sbin/start-balancer.sh -threshold <percentage of disk capacity>percentage of disk capa ...