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 ...
随机推荐
- 【大数处理、正则表达式】NYOJ-513
[正则] 正则表达式是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为“元字符”). 模式描述在搜索文本时要匹配的一个或多个字符串. 常用字符: //正则表达式 //$ 匹配 ...
- 【转】cocos2d-x与ios内存管理分析(在游戏中减少内存压力)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=281 注:自己以前也写过coco ...
- 一步一步ITextSharp 低级操作函数使用
首先说一下PDF文档的结构: 分为四层,第一层和第四层由低级操作来进行操作,第二层.第三层由高级对象操作 第一层操作只能使用PdfWriter.DirectContent操作,第四层使用DirectC ...
- MySQL 5.6 复制:GTID 的优点和限制(第一部分)
全局事务标示符(Global Transactions Identifier)是MySQL 5.6复制的一个新特性.它为维护特定的复制拓扑结构下服务器的DBA们大幅度改善他们的工作状况提供了多种可能性 ...
- linux编程获取本机网络相关参数
getifaddrs()和struct ifaddrs的使用,获取本机IP 博客分类: Linux C编程 ifaddrs结构体定义如下: struct ifaddrs { struct ifad ...
- POJ 1080 Human Gene Functions
题意:给两个DNA序列,在这两个DNA序列中插入若干个'-',使两段序列长度相等,对应位置的两个符号的得分规则给出,求最高得分. 解法:dp.dp[i][j]表示第一个字符串s1的前i个字符和第二个字 ...
- java web 学习九(通过servlet生成验证码图片)
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- MyBatis 入门到精通(三) 高级结果映射
MyBatis的创建基于这样一个思想:数据库并不是您想怎样就怎样的.虽然我们希望所有的数据库遵守第三范式或BCNF(修正的第三范式),但它们不是.如果有一个数据库能够完美映射到所有应用程序,也将是非常 ...
- 总结c++ primer中的notes
转载:http://blog.csdn.net/ace_fei/article/details/7386517 说明: C++ Primer, Fourth Edition (中英文)下载地址:htt ...
- alibaba笔试
1.D A(7,7)/(A(3,3)A(2,2)) = 420 关联: 字符串的排列和组合:http://blog.csdn.net/hackbuteer1/article/details/7462 ...