P1087 FBI树 [2004普及]

这是个正常的、很简单的分治,然后我成功地将这个题搞成了一个贼难搞的东西

还是说一下我那个非常麻烦的思路:

  1. 建树  2. 后序遍历

然后就在建树的过程中死循环了,然后还一堆毛病

看了一个AC代码,该成这个了

#include<iostream>
#include<cstdio>
#include<cstring>
#define NUM 1010
#define rr register
using namespace std;
char a[NUM];
int n,cnt;void work( int l,int r ){
    int mid = (l+r+1) >> 1;
    if( l < r ){
        work( l,mid-1 );
        work( mid,r );
    }
    if( l == r ){
        if( a[l] == '0' ) cout << "B";
        else cout << "I";
        return;
    }
    bool yi = 0,ling = 0;
    for( int i = l;i <= r;i++ ){
        if( !yi && !ling ){
            if( a[i] == '1' ) yi = 1;
            else ling = 1;
        }else if( (yi && a[i] == '0') || (ling && a[i] == '1') ){
            cout << "F";
            return;
        }
    }
    if( yi ) cout << "I";
    else cout << "B";
    return;
}
int main(){
    ios::sync_with_stdio(0);
    cin >> n >> a;
    work( 0,strlen( a )-1 );
    return 0;
}

就是直接输出就好了,也不用存,

后序遍历就遍历吧,也是不难

posted @ 2021-09-24 17:58  little_sheep_xiaoen  阅读(27)  评论(0编辑  收藏  举报