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;} 


原创粉丝点击