【NOIP2004】【Luogu1087】FBI树

problem

solution

codes

#include<stdio.h>
#define maxn 1<<12
using namespace std;
struct node{
    char t;
    int l, r;
    node():t('*'),l(0),r(0){}
}tree[maxn];
char s[maxn];
int top = -1;
char check(int le, int ri){
    int l=0, b=0;
    for(int i = le; i <= ri; i++){
        if(s[i]=='1')l=1;
        if(s[i]=='0')b=1;
    }
    if(l==1&&b==1)return 'F';
    if(l)return 'I';
    if(b)return 'B';
}
int build(int l, int r){
    if(l >= r){
        tree[++top].t = check(l,r);
        return top;
    }
    tree[++top].t = check(l,r);
    int now = top;
    int root = (l+r)/2;
    tree[now].l = build(l,root);
    tree[now].r = build(root+1,r);
    //printf("root:%d  top:%d
",root,top+1);
    //printf("now:%d  lch:%d  rch:%d
",now, tree[now].l,tree[now].r);
    return now;
}
void dfs(int root){
    //printf("root:%d %d %d
",root,tree[root].l,tree[root].r);
    if(tree[root].l)dfs(tree[root].l);
    if(tree[root].r)dfs(tree[root].r);
    printf("%c",tree[root].t);
}
int main(){
    //freopen("data.in","r",stdin);
    int n;  scanf("%d%s",&n,s+1);
    n = 1<<n;
    build(1,n);
    dfs(0);
    return 0;
}
原文地址:https://www.cnblogs.com/gwj1314/p/9444692.html