FBI树

传送门(又是一道很水的题)

这题我竟然还使用了类似线段树的思想……就是先递归到最底下一层之后读入,然后依次向递归返回更新答案。

感觉没啥可说的……直接看代码吧。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<set>
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('
')

using namespace std;
typedef long long ll;
const int M = 5000005;
const int INF = 1000000009;

int read()
{
    int ans = 0,op = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
    if(ch == '-') op = -1;
    ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
    ans *= 10;
    ans += ch - '0';
    ch = getchar();
    }
    return ans * op;
}

int n,len,b[10005];
char s[10005];

void dfs(int x,int p)
{
    if(x == 1)
    {
    char c = getchar();
    if(c == '1') b[p] = 1,printf("I");
    else b[p] = 0,printf("B");
    return;
    }
    dfs(x>>1,p<<1);
    dfs(x>>1,p<<1|1);
    if(b[p<<1] == 0 && b[p<<1|1] == 0) b[p] = 0,printf("B");
    else if(b[p<<1] == 1 && b[p<<1|1] == 1) b[p] = 1,printf("I");
    else b[p] = 2,printf("F");
}

int main()
{
    n = read();
    len = 1<<n;
    getchar();
    dfs(len,1);
    return 0;
}
    
原文地址:https://www.cnblogs.com/captain1/p/9636684.html