FBI Tree

FBI树,直接套上线段树的模板

#include<bits/stdc++.h>
#define endl '
'
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const int N = 1055;
typedef long long ll; 
string s; 
struct node{
    int l,r,type;
}T[N];
string res="";
void Build( int v,int l,int r ){
    T[v].l = l,T[v].r = r; 
    if( l==r ){
        if( s[l]=='0' ) T[v].type = 0;
        else T[v].type = 1;
        return ;
    }
    int mid = (l+r)/2;
    Build( v*2,l,mid );
    Build( v*2+1,mid+1,r );
    if( T[v*2].type==T[v*2+1].type ) T[v].type = T[v*2].type;
    else T[v].type= 2;
}
void aftervis(int v){
    if( T[v].l!=T[v].r ){
        aftervis(v*2);
        aftervis(v*2+1);
    } 
    if( T[v].type==2 ) cout<<'F';
    else if( T[v].type==0 ) cout<<'B';
    else cout<<'I';
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);  
    int t; cin>>t;
    while(t--){
        int n; cin>>n;
        int len = 1<<(n);
        cin>>s; s=" "+s;
        Build( 1,1,len );
        aftervis(1); 
        cout<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/SunChuangYu/p/12458458.html