FBI tree
there is more than one way to solve problems, and perhaps a better solution. It's a moment of inspiration .Description We can divide the string consisting of "0" and "1" into three categories: all "0" strings are called B-strings, all "1" strings are called I-strings, and strings containing "0" and "1" are called F-strings.
The FBI tree is a binary tree [1], and its node types also include F-nodes, B-nodes, and I-nodes three species. By a 2N length of "01" string s can be constructed out of an FBI tree T, recursive construction method is as follows:
1) the root node of T is R, and its type is the same as the type of string s;
2) If the length of the string s is greater than 1, the string s is separated from the middle, divided into equal-length left and right substrings of S1 and S2; the left subtree of R is constructed by the left substring S1 T1, and the right sub-tree of R is S2 by the right child string.
Now given a "01" string of length 2N, construct an FBI tree with the above construction method and output its sequential traversal [2] sequence. The first line of the input file fbi.in is an integer N (0 <= N <= 10), and the second row is a "01" string with a length of 2N. The output file Fbi.out includes a line that contains only one string, which is the post-order traversal sequence of the FBI tree. Sample Input
310001011
Sample Output
Ibfbbbfibfiiiff
The
problem was something I thought about for some time. Saw YG and WYC ideas, are achievements after the post-order traversal, I was thinking, can not build, because of its own achievements of a process is the process of traversal, but the order of the traversal is not the same. It was later discovered that it was possible to achieve it entirely by recursion. The traversal order for a set of data strings S is this: s[0]->s[1]->s[0]+[s1]->s[2]->s[3]->s[2]+s[3]->s[0]+s[1]+s[2]+s[3] ... The code is as follows:
#include <cstdio> #include <cstring>char s[1100];int FBI (int start,int len) {//Two variables, start start position, len length if (len==1) { if (s[start]== ' 0 ') { printf ("B"); return 0; } else{ printf ("I"); return 1; } } else{//return 0 Description is all 0, return 1 description is all 1, otherwise return 2 int x=fbi (START,LEN/2); int Y=FBI (START+LEN/2,LEN/2); if (x==y&&x==0) { printf ("B"); return 0; } else if (x==y&&x==1) { printf ("I"); return 1; } else{ printf ("F"); return 2;}} } int main () { int n;//actually does this, n is a useless variable scanf ("%d%s", &n,s); int L=strlen (s); FBI (0,l); printf ("\ n"); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sdnu 1168.FBI Tree "NOIP 2004 popularization Group" "Not Achievements" "July 28"