FBI树

题目描述 Description

我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串。

FBI树是一种二叉树[1],它的结点类型也包括F结点,B结点和I结点三种。由一个长度为2N的“01”串S可以构造出一棵FBI树T,递归的构造方法如下:

1) T的根结点为R,其类型与串S的类型相同;

2) 若串S的长度大于1,将串S从中间分开,分为等长的左右子串S1和S2;由左子串S1构造R的左子树T1,由右子串S2构造R的右子树T2。

现在给定一个长度为2N的“01”串,请用上述构造方法构造出一棵FBI树,并输出它的后序遍历[2]序列。

输入描述 Input Description

输入的第一行是一个整数N(0 <= N <= 10),第二行是一个长度为2N的“01”串。

输出描述 Output Description

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

样例输入 Sample Input

3

10001011

样例输出 Sample Output

IBFBBBFIBFIIIFF

数据范围及提示 Data Size & Hint

对于40%的数据,N <= 2;

对于全部的数据,N <= 10。

 

一看题我就疯了。。。交了三遍。。。

分类讨论n=0和n>=1

n=0时,直接判断输出

n>0时,递归建树,后续遍历判断输出。

type node=record
                data:ansistring;
                lson,rson:longint;
                end;

var tree:array[1..10000]of node;
      i,j,k,l,n,m:longint;
      choose0,choose1:boolean;
      ch:char;

procedure print;                                         {判断输出}
               begin if choose0 and choose1
                          then write('F');
                        if choose0 and not choose1
                           then write('B');
                        if not choose0 and choose1
                           then write('I');
               end;

procedure dfs(s,m:longint);                        {递归建树}
                var i,j,k,l:longint;
                     choose0,choose1:boolean;
                begin if m>=1
                           then begin tree[s].lson:=s*2;
                                           tree[s].rson:=s*2+1;
                                           tree[tree[s].lson].data:=copy(tree[s].data,1,m div 2);
                                           tree[tree[s].rson].data:=copy(tree[s].data,m div 2+1, m div 2);
                                           dfs(tree[s].lson,m div 2);
                                           choose0:=false; choose1:=false;
                                           for i:=1 to length(tree[tree[s].lson].data) do
                                                begin if tree[tree[s].lson].data[i]='0'
                                                           then choose0:=true;
                                                         if tree[tree[s].lson].data[i]='1'
                                                            then choose1:=true;
                                                end;
                                           if choose0 and choose1
                                              then write('F');
                                           if choose0 and not choose1
                                              then write('B');
                                           if not choose0 and choose1
                                             then write('I');
                                          dfs(tree[s].rson,m div 2);
                                          choose0:=false; choose1:=false;
                                          for i:=1 to length(tree[tree[s].rson].data) do
                                               begin if tree[tree[s].rson].data[i]='0'
                                                          then choose0:=true;
                                                        if tree[tree[s].rson].data[i]='1'
                                                           then choose1:=true;
                                               end;
                                          if choose0 and choose1
                                             then write('F');
                                          if choose0 and not choose1
                                             then write('B');
                                          if not choose0 and choose1
                                             then write('I');
                                 end;
                end;

begin readln(n);
         if n=0                                             {判断n=0时的特殊情况}
           then begin read(ch);
                           if ch='1'
                             then begin writeln('I');
                                             halt;
                                     end
                             else begin writeln('B');
                                             halt;
                                   end;
                  end;
        m:=1;
        for i:=1 to n do              {求字符长度}
             m:=m*2;
        readln(tree[1].data);
        tree[1].lson:=2;
        tree[1].rson:=3;
        tree[2].data:=copy(tree[1].data,1,m div 2);
        tree[3].data:=copy(tree[1].data,m div 2+1,m div 2);
        dfs(2,m div 2);
        choose0:=false;          {判断树根的左儿子}
        choose1:=false;
        for i:=1 to m div 2 do
             begin if tree[2].data[i]='0'
                        then choose0:=true;
                     if tree[2].data[i]='1'
                        then choose1:=true;
             end;
        print;
        dfs(3,m div 2);
        choose0:=false; choose1:=false;   {判断树根的右儿子}
        for i:=1 to m div 2 do
             begin if tree[3].data[i]='0'
                        then choose0:=true;
                      if tree[3].data[i]='1'
                        then choose1:=true;
             end;
        print;
        choose0:=false; choose1:=false;             {判断树的根}
        for i:=1 to m do
             begin if tree[1].data[i]='0'
                        then choose0:=true;
                     if tree[1].data[i]='1'
                        then choose1:=true;
             end;
        print;
       writeln;
end.

posted @ 2015-01-17 14:05  紫蜘蛛之歌  阅读(162)  评论(0编辑  收藏  举报