UVa 712 S-Trees
来源:互联网 发布:培训矩阵图 编辑:程序博客网 时间:2024/06/07 00:30
思路分析:
每个查询可以看做一个2进制数,其对应的10进制数即为对应的叶子位置。注意输入的查询顺序都是x1 x2 ... 的正序,要根据给的x顺序调整。
注意:
输入叶子值序列时候,不可用int型整数存储,会溢出,因此要用字符串数组
题解:
#include <cstdio>const int MAX = 1<<7+1;int seq[MAX], order[MAX], q[MAX], ans[MAX];int n, k, m = 1;int main(){#ifdef LOCALfreopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);#endifwhile(scanf("%d\n", &n) && n){int num = 1<<n, count = 0;for(int i = 1; i <= n; i++){int pos;if(i != n) scanf("x%d ", &pos);else scanf("x%d", &pos);order[pos] = i;}int s;char str[MAX];scanf("%s", &str);for(int i = num-1; i >= 0; i--){seq[i] = str[i] - '0';}scanf("%d", &k);count = 0;while(k--){scanf("%d", &s);for(int i = n; i >= 1; i--){q[order[i]] = s % 10;s = s / 10;}int index = 0;for(int i = 1; i <= n; i++){index = index*2 + q[i];}ans[count++] = seq[index];}printf("S-Tree #%d:\n", m++);for(int i = 0; i < count; i++) printf("%d", ans[i]);printf("\n\n");}return 0;}
阅读全文
0 0
- UVa 712 S-Trees
- UVa 712 - S-Trees
- Uva 712 S-Trees
- uva 712 - S-Trees
- uva 712 - S-Trees
- UVa 712 - S-Trees
- UVA 712 S-Trees
- uva-712 S-Trees
- uva 712-S-Trees
- UVA 712 S-Trees
- uva 712 S-Trees
- uva 712 S-Trees
- UVa 712 - S-Trees
- UVA 712 - S-Trees
- UVA 712 S-Trees
- UVA 712 S-Trees
- UVA - 712 S-Trees
- UVA 712 - S-Trees
- Python正则表达式
- hdu6197array array array(最长上升子序列二分版)
- ubuntu 的基本命令
- GUI1
- 常用数学符号的 LaTeX 表示方法
- UVa 712 S-Trees
- mysql explain 的type解释
- Android 7.1 应用快捷方式(ShortcutManager的使用)
- SpringMVC实现查询结果下载为CSV文件
- [python]leetcode(105). Construct Binary Tree from Preorder and Inorder Traversal
- 直接插入、冒泡、快速、简单选择、堆、归并排序算法
- JVM内存区域和内存溢出异常
- 3301: [USACO2011 Feb] Cow Line 康托展开
- HTML5拖放