首页 > 代码库 > UVA - 712 S-Trees(S树)

UVA - 712 S-Trees(S树)

题意:0往左走,1往右走,已知所有叶子的值,每个查询都是根结点到叶子结点的路径,路径的每一个点分别对应着x1,x2,x3……但是实际上的S树的路径可能并非是x1,x2,x3……

分析:先存路径变量的顺序,来控制最后访问的顺序。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 128 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char s[MAXN];
char t[MAXN];
vector<int> order;
vector<char> v[10];
int POW[10];
void init(){//2的i次方
    int tmp = 1;
    for(int i = 1; i <= 7; ++i){
        tmp *= 2;
        POW[i] = tmp;
    }
}
int main(){
    int n;
    int kase = 0;
    init();
    while(scanf("%d", &n) == 1){
        if(n == 0) return 0;
        for(int i = 0; i < 10; ++i) v[i].clear();
        order.clear();
        for(int i = 0; i < n; ++i){
            char tmp[3];
            scanf("%s", tmp);
            order.push_back(tmp[1] - 0);//S树的变量顺序
        }
        scanf("%s", s);
        int m;
        scanf("%d", &m);
        for(int k = 0; k < m; ++k){
            scanf("%s", t);
            for(int i = 0; i < n; ++i){
                v[i + 1].push_back(t[i] - 0);//存x[i+1]的m次查询的值
            }
        }
        printf("S-Tree #%d:\n", ++kase);
        for(int i = 0; i < m; ++i){
            int cnt = 1;//根结点标号为1
            for(int j = 0; j < n; ++j){
                 if(!v[order[j]][i]){//若为0,则2i;否则2i+1
                    cnt *= 2;
                 }
                 else{
                    cnt = cnt * 2 + 1;
                 }
            }
            printf("%c", s[cnt - POW[n]]);
        }
        printf("\n\n");
    }
    return 0;
}

 

UVA - 712 S-Trees(S树)