算法训练 FBI树

 算法训练 FBI树  
时间限制:1.0s   内存限制:256.0MB
       
问题描述
  我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串。
  FBI树是一种二叉树,它的结点类型也包括F结点,B结点和I结点三种。由一个长度为2 N的“01”串S可以构造出一棵FBI树T,递归的构造方法如下:
  1)T的根结点为R,其类型与串S的类型相同;
  2)若串S的长度大于1,将串S从中间分开,分为等长的左右子串S1和S2;由左子串S1构造R的左子树T1,由右子串S2构造R的右子树T2。
  现在给定一个长度为2 N的“01”串,请用上述构造方法构造出一棵FBI树,并输出它的后序遍历序列。
输入格式
  第一行是一个整数N(0 <= N <= 10),第二行是一个长度为2 N的“01”串。
输出格式
  包括一行,这一行只包含一个字符串,即FBI树的后序遍历序列。
样例输入
3
10001011
样例输出
IBFBBBFIBFIIIFF
数据规模和约定
  对于40%的数据,N <= 2;
  对于全部的数据,N <= 10。
  注:
  [1] 二叉树:二叉树是结点的有限集合,这个集合或为空集,或由一个根结点和两棵不相交的二叉树组成。这两棵不相交的二叉树分别称为这个根结点的左子树和右子树。
  [2] 后序遍历:后序遍历是深度优先遍历二叉树的一种方法,它的递归定义是:先后序遍历左子树,再后序遍历右子树,最后访问根。

#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>

#define MAX 5000


using namespace std;

typedef struct Node {
    char ch;
    string str;
    int left;
    int right;
    int id;
    Node() {
        id = -1;
        ch = '#';
        str = "";
        left = right = -1;
    }
} Node;

Node node[MAX];
int Max = 1;

char fun( string str ) {
    bool hasOne = false;
    bool hasZero = false;
    if( str.find( '1', 0 ) != string::npos ) hasOne = true;
    if( str.find( '0', 0 ) != string::npos ) hasZero = true;

    if( hasOne && hasZero ) return 'F';
    if( hasOne && !hasZero ) return 'I';
    if( !hasOne && hasZero ) return 'B';
}

void createTree( int n, string str ) {
    queue<Node> q;
    node[1].id = 1;
    node[1].str = str;
    q.push( node[1] );

    while( !q.empty() ) {

        Node curNode = q.front();
        q.pop();
        int id = curNode.id;
        if( id > Max ) break;
        string curStr = curNode.str;
        string leftStr = curStr.substr( 0, curStr.size() / 2 );
        string rightStr = curStr.substr( curStr.size() / 2 );
        node[id].ch = fun( curNode.str );
        int leftId = 2 * id;
        int rightId = 2 * id + 1;
        //printf( "%d %c\n", node[id].id, node[id].ch );
        //if( id * 2 + 1 <= Max ) {
            node[id].left = leftId;
            node[id].right = rightId;
            node[leftId].id = leftId;
            node[leftId].str = leftStr;
            node[rightId].id = rightId;
            node[rightId].str = rightStr;
            q.push( node[leftId] );
            q.push( node[rightId] );
        //}
    }
}

void dfs( int curIndex ) {
    if( node[curIndex].left == -1 && node[curIndex].right == -1 ) return;

    dfs( node[curIndex].left );
    dfs( node[curIndex].right );
    printf( "%c", node[curIndex].ch );
}

int main() {
    //freopen( "123.txt", "r", stdin );
    int n;
    string str;
    cin >> n >> str;

    for( int i = 0; i <= n; i++ ) {
        Max = Max * 2;
    }
    Max--;

    //printf( "%d\n", Max );
    createTree( n, str );
    dfs( 1 );
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值