水题一道,没有用到特别的方法。
根据 000,010,111,110 这些二进制数字转化为十进制得知该查询是哪一个叶子节点的值。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int maxn = 10+2;
int n,m;
int label[maxn];
int order[maxn];
int res[1<<maxn];
string ans;
int main(){
int k = 1;
while(cin>>n && n!=0){
memset(label,0,sizeof(label));
memset(res,-1,sizeof(res));
ans = "";
string str;
for(int i=1;i<=n;i++){
cin >> str;
label[i] = str[1] - ‘0‘;
}
cin >> str;
for(int j=0;j<(1<<n);j++){
res[j] = str[j] - ‘0‘;
}
cin >> m;
while(m--){
cin >> str;
int cnt = 0;
for(int i=1;i<=n;i++){
order[i] = str[label[i]-1] - ‘0‘;
}
/*
cout<<"label:"<<" ";
for(int i=1;i<=n;i++){
cout << label[i] << " ";
}
cout<<"order:"<<" ";
for(int i=1;i<=n;i++){
cout << order[i] <<" ";
}
*/
for(int i=1;i<=n;i++){
cnt = cnt*2+order[i];
}
ans += res[cnt] + ‘0‘;
}
//cout<<"S-Tree #1:"<<endl;
printf("S-Tree #%d:\n",k);k++;
cout<<ans;
cout<<"\n\n";
}
return 0;
}
时间: 2024-11-20 21:34:17