NOIP2004T3FBI tree problem-solving report

See the original title Luo Valley ( https://www.luogu.org/problem/show?pid=1087 )
we can put a "0" and "1" string components are classified into three categories: all "0", said string B string is the string, all "1" sequence called I string, contains both "0" and contains "1" is referred to as F series.

FBI tree is a binary tree, it also includes a node type nodes F, B and node I node three. A length of 2 ^ N "01" in a string S can be constructed FBI tree T, constructed recursively as follows:

1) T is the root node R, the same type and the type of string S;

2) If a run length greater than S, the string S parted in the middle is divided into left and right sub-strings of equal length S1 and S2; T1 left by the left subtree structure R is substring S1, S2 by the right substrings in R configuration right subtree T2.

Now given a length of 2 ^ N "01" series, construct the above-described one FBI tree construction method, and outputs it postorder traversal sequence.

First we consider to be the order traversal, that is to traverse the left subtree, then traverse the right subtree, then traverse father.
So we then divide and conquer recursive process may be selected (l, l + (rl) / 2) and (1 + l + (rl) / 2, r) in both cases.
The first is the left subtree, the second is the right subtree.
Recursive to only one time is l == r when reaching the limit value.
We can end the recursive changed output.
Several tmp represented by a 0
with a tmp1 1 represents a few
that we know
when tmp = 0, 0 0, indicating that all substring 1, the output I
Conversely, when tmp1 time = 0, 1 has 0, indicates that the substring are all 0, the output B
when tmp = 0 and tmp1 = 0 when there are a substring both 0, the output F.!!
code is as follows:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

const int maxn=10000;
int n,a1[maxn<<1],l;
char a[maxn<<1];

void work(int l,int r){
    if(l<r){
        work(l,l+(r-l)/2);
        work(1+l+(r-l)/2,r);
    }
    int cnt=0;
    int cnt1=0;
    for(int i=l;i<=r;i++){
        if(a1[i]){
            cnt1++;
        }
        else{
            cnt++;
        }
    }
    if(!cnt){
        printf("I");
    }
    else if(!cnt1){
        printf("B");
    }
    else{
        printf("F");
    }
}

int main(){
    scanf("%d",&n);
    scanf("%s",&a[1]);
    l=strlen(&a[1]);
    for(int i=1;i<=l;i++){
        a1[i]=a[i]-'0';
    }   
    work(1,l);
    return 0;   
}
Published 41 original articles · won praise 58 · views 60000 +

Guess you like

Origin blog.csdn.net/a1351937368/article/details/78001903