P1087

洛谷 P1087 题解

在这里插入图片描述
思路是讲串一次次对分,分成左孩子和右孩子,直到串的长度只剩下1

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <map>
#include <queue>
#include <cstring>
#include <cmath>

using namespace std;

string s;

void tree(int x,int y)
{
    if(y>x){								//判断是否只剩下1份
        tree(x,(x+y)/2);				 //左边
        tree((x+y)/2+1,y);			//右边
    }
    int B=1,I=1;
    //判断串是属于I B F的哪一种。
    for(int i=x; i<=y; i++){
        if(s[i]=='1')   B=0;
        else if(s[i]=='0')  I=0;
    }
    if(B)   cout << 'B';
    else if(I)  cout << 'I';
    else    cout << 'F';
}

int main()
{
    int n;
    cin >> n >> s;
    tree(0,pow(2,n)-1);			//pow函数计算2^n
    cout << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/chenchenchenhk/article/details/88955600