#include <iostream>
#include <string>

using namespace std;

// 递归构造FBI树
string constructFBITree(string s) {
    // 如果字符串长度为1,则直接返回对应的结点类型
    if (s.length() == 1) {
        if (s == "0") {
            return "B";
        } else {
            return "I";
        }
    }

    // 分割字符串
    int mid = s.length() / 2;
    string leftStr = s.substr(0, mid);
    string rightStr = s.substr(mid);

    // 构造左右子树
    string leftTree = constructFBITree(leftStr);
    string rightTree = constructFBITree(rightStr);

    // 根据左右子树的类型确定根结点的类型
    if (leftTree == rightTree) {
        return leftTree;
    } else {
        return "F";
    }
}

// 后序遍历FBI树
void postOrderTraversal(string tree, string& result) {
    if (tree.length() == 1) {
        result += tree;
        return;
    }

    int mid = tree.length() / 2;
    string leftTree = tree.substr(0, mid);
    string rightTree = tree.substr(mid);

    postOrderTraversal(leftTree, result);
    postOrderTraversal(rightTree, result);
    result += tree;
}

int main() {
    int N;
    string s;
    cin >> N >> s;

    string tree = constructFBITree(s);
    string result = "";
    postOrderTraversal(tree, result);

    cout << result << endl;

    return 0;
}
``

标签: 科技


原文地址: https://gggwd.com/t/topic/hRWc 著作权归作者所有。请勿转载和采集!