天天看點

UVa712_S-Trees

題目:點選打開連結

思路:對于一節點k,其左右節點編号分别為2k,2k+1。然後模拟題意即可。原題輸入比較多,但輸入X1、X2.....沒有用到。

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<stack>
#include<vector>
#include<queue>
#include<cstring>
#include<sstream>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

int main(){
	//freopen("random_numbers.txt","r",stdin);
	int n,kase=0;
	string x[10];//存X1,X2....Xn的值,但下面程式并不需要        
	char s[200],x_in[10];
	while (cin >> n&&n){		
		for (int i = 0; i < n; i++)	cin >> x[i];  
		for (int i = 0; i < (1 << n); i++) cin >> s[i];  //根節點的值
		int T; cin >> T;
		cout << "S-Tree #" << ++kase << ":\n";		
		while (T--){
			for (int i = 0; i <n; i++) cin >> x_in[i];  
			int k = 1;
			for (int i = 0; i <n; i++){
				k = x_in[i]=='0' ? 2 * k : 2 * k + 1;    //根據每個節點的值選擇向“左”還是“右”
			}
			cout << s[k-(1<<n)];
		}
		cout << endl<<endl;
	}
	return 0;
}