Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.2 kB
0
Indexable
Never
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct Node_{
    int data;
    struct Node_ *left,*right;
}Node;

char str[14000];
int pos,longer;
char table[3005];

Node* make(int num){
    Node* node=(Node*)malloc(sizeof(Node));
    node->data=num;
    node->left=node->right=NULL;
    return node;
}

Node* constructTree(void){
    int num=0;
    while(isdigit(str[pos]) && pos<longer){
        num=num*10+(str[pos]-'0');
        pos++;
    }
    Node* root=make(num);
    if(str[pos]=='?'){
        pos++;
        root->left=constructTree();
        root->right=constructTree();
        return root;
    }

    pos++;
    return root;
}

char Ans(Node* root){
    if(root->left==NULL && root->right==NULL) return table[root->data-1];

    if(table[root->data-1]=='1') return Ans(root->left);
    else return Ans(root->right);
}

int main(void){
    int N;
    scanf("%s",str);
    pos=0;
    longer=strlen(str);
    Node *root=constructTree();

    scanf("%d",&N);
    for(int i=0;i<N;i++){
        scanf("%s",table);
        printf("%c\n",Ans(root));
    }
    return 0;
}