1
2023-12-30 18:52:09
发布于:北京
#include<bits/stdc++.h>
using namespace std;
const int N=1e5;
string tree[N];
string s;
int q=1;
int n;
void fbi(string ss,int q){
//cout<<" login ";
// cout<<ss<<" ";
string s1="",s2="";
for(int i=0;i<ss.length()/2;i++){
s1+=ss[i];
s2+=ss[i+ss.length()/2];
}
int flag1=0,flag2=0;
//cout<<s1<<" "<<s2<<" ";
for(int i=0;i<s1.length();i++){
//cout<<i<<" ";
if(s1[i]=='1')flag1++;
else if(s1[i]=='0')flag1--;
//cout<<flag1<<"&";
if(s2[i]=='1')flag2++;
else if(s2[i]=='0')flag2--;
//cout<<flag2<<" ";
}
int tp=~s1.length();
// cout<<"("<<flag1<<" "<<flag2<<'('<<s1.length()<<" "<<tp+1<<endl;
if(flag1==s1.length())tree[2*q]='I';
else if(flag1==tp+1)tree[2*q]='B';
else tree[2*q]='F';
if(flag2==s2.length())tree[2*q+1]='I';
else if(flag2==tp+1){tree[2*q+1]='B';}
else tree[2*q+1]='F';
//cout<<q<<" ";
if(s1.length()>1)fbi(s1,2*q);
//cout<<endl<<"第二部分"<<endl;
//cout<<s2<<" ";
if(s2.length()>1)fbi(s2,2*q+1);
}
void dfs(int w){
if(2*w<=pow(2,n+1)-1)dfs(2*w);
if(2*w+1<=pow(2,n+1)-1)dfs(2*w+1);
cout<<tree[w];
}
int main(){
cin>>n;
cin>>s;
int flag=0;
for(int i=0;i<s.length();i++){
if(s[i]=='1')flag++;
else if(s[i]=='0')flag--;
}
if(flag==s.length())tree[1]='I';
else if(flag==-s.length())tree[1]='B';
else tree[1]='F';
fbi(s,1);
//cout<<111111;
dfs(1);
}
这里空空如也
有帮助,赞一个