题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=653
Sample Input
3
x1 x2 x3
00000111
4
000
010
111
110
3
x3 x1 x2
00010011
4
000
010
111
110
0
Sample Output
S-Tree #1: 0011
S-Tree #2: 0011
题解:完全二叉树,数据不大,用编号判断即可。
代码如下:
#include<bits/stdc++.h>
using namespace std;
const int N = 5050;
int x[N];
char s[N];
char dep[N];
int ans[N];
int main(){
int time=0;
int n;
while(~scanf("%d",&n) && n){
getchar();
for(int i=0;i<n;i++){
getchar();
scanf("%d",&x[i]);
getchar();
}
scanf("%s",s);
long long bian=pow(2,n);
// printf("%d\n",bian);
int count;
scanf("%d",&count);
for(int i=0;i<count;i++){
scanf("%s",dep);
long long ans1=1;
for(int j=0;j<n;j++){
if(dep[x[j]-1]=='0')
ans1=2*ans1;
if(dep[x[j]-1]=='1')
ans1=ans1*2+1;
}
ans[i]=s[ans1-bian]-'0';
}
printf("S-Tree #%d:\n",++time);
for(int i=0;i<count;i++)
printf("%d",ans[i]);
printf("\n");
printf("\n");
}
return 0;
}