东华大学2022复试上机最后80题 ---48 FBI树

                          48 FBI树

我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串。
  FBI树是一种二叉树,它的结点类型也包括F结点,B结点和I结点三种。由一个长度为2N的“01”串S可以构造出一棵FBI树T,递归的构造方法如下:
  1)T的根结点对应的内容为S,因此其类型与串S的类型相同;
  2)若串S的长度大于1,将串S从中间分开,分为等长的左右子串S1和S2;由左子串S1构造左子树T1,由右子串S2构造右子树T2。
  现在给定一个长度为2N的“01”串,请用上述构造方法构造出一棵FBI树,并输出它的后序遍历序列。
第一行是一个整数N(0 <= N <= 10),第二行是一个长度为2N的“01”串。

输出说明 :

包括一行,这一行只包含一个字符串,即FBI树的后序遍历序列。

输入
3
10001011

输出

IBFBBBFIBFIIIFF

自我感觉写的代码很秒

#include<bits/stdc++.h>
#include<bitset>
#include<unordered_map>
#define pb push_back
#define bp __builtin_popcount
#define TIME cout << "RuningTime: " << clock() << "ms\n", 0
#define ls x<<1
#define rs x<<1|1
using namespace std;
typedef  long long ll;
const int inf=0x3f3f3f3f;
const int maxn=2e3+10;
const int MOD=9901;
const int mod = 50000;
const double PI=3.14;
int lowbit(int x){return x&-x;}
ll gcd(ll x, ll y){ return y == 0 ? x : gcd(y, x%y); }

ll lcm(ll x, ll y){ return x / gcd(x, y)*y; }
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
inline ll fpow(ll a, ll b,ll p){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % p; b >>= 1; t = (t*t) % p; }return r; }
priority_queue<int, vector<int>, greater<int> > q;

string f[maxn];
void build(int x,string st)
{
    int len=st.length();
    f[x]=st;
    if(len==1)
    {
        return ;
    }
    
    build(x*2,st.substr(0,len/2));
    build(x*2+1,st.substr(len/2,len/2));

}
char solve(string str)
{
    int f1=0,f2=0;
    for(int i=0;i<str.length();i++)
    {
        if(str[i]=='0')f1=1;
        if(str[i]=='1')f2=1;

    }
    if(f1&&f2)return 'F';
    else if(f1)return 'B';
    else return 'I';
}
void dfs(int x)
{
    if(f[x].length()==1)
    {
        cout<<solve(f[x]);
        return ;
    }
    dfs(x*2);
    dfs(x*2+1);
    cout<<solve(f[x]);
}
int main()
{

    int n;
    cin>>n;
    string str;
    cin>>str;
    build(1,str);

    dfs(1);

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值