Uva 712 S Tree

Uva 712 S Tree

Subject description:

It gives a full binary tree, each layer representing a 01 variable, go left when set to 0, the right time to take a walk. Each with a variable \ (x_i \) represents, then this may be equivalent to several tree \ (x_i \) bit operation between the given values of all of the leaf nodes and some queries ( \ (x_i \) values ), evaluates each leaf node queries arrive.

Topic links: https://vjudge.net/problem/UVA-712

Ideas:

Conclusion previously used a purple book, that is a full binary numbers, the left child node and the right child node of the node k numbers are 2k and 2k + 1. According to the input format of the subject, to determine the path for each query, because \ (x_1, x_2, ..., x_i \) a different order of appearance, the input query path will later converted accordingly. This question I mixed up with the input getchar()and cin, written somewhat cumbersome, in fact, directly to each line as a stringread on it.

Code:
#include <iostream>
#include <memory.h>
using namespace std;
const int maxn = 7 + 2;
int main()
{
    int n;int kase = 0;
//  freopen("uva712_in.txt", "r", stdin);
//  freopen("uva712_out.txt", "w", stdout);
    while(cin >> n && n){
        int s[maxn], next[maxn];
        int depth = n;
        s[0] = 0, next[0] = 0;
        int i = 1;
        ++kase;
        printf("S-Tree #%d:\n", kase);
        while(n--){
            string str; cin >> str;
            next[i] = next[i-1];
            next[i-1] = str[1]-'0';
            ++i;
        }
        int terminal[1<<maxn];
        memset(terminal, 0, sizeof(terminal));
        char c; while((c=getchar()) == '\n');
        for(int i = 0; i < 1<<depth; ++i){
            terminal[i] = c - '0';
            c = getchar();  
        }
        
        int m; cin >> m;
        int path[maxn], p[maxn];
        while(m--){
            int i = 0; char c;
            memset(path, 0, sizeof(path));
            memset(p, 0, sizeof(p));
            while((c = getchar()) == '\n');
            while(c != '\n'){
                p[i] = c - '0';
                ++i;
                c = getchar();
            }
            for(int i = 0; i < depth; ++i){
                path[i] = p[next[i]-1];
            }
            int start = 1;
            for(int i = 0; i < depth; ++i){
                if(path[i]) start = start * 2 + 1;
                else start = start * 2;
            }
            int end = start - (1 << depth);
            cout << terminal[end];
        }
        cout << "\n" << "\n";
    }
}

Guess you like

Origin www.cnblogs.com/patrolli/p/11291230.html