You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.7 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
//需要开大一点否则RE,得了90分。
const int N = 1e5 + 10;
/**
1011
1011
10 11
1 0 1 1
F7
F3 I6
I1 B2 I4 I5
IBFIIIF(
1.1011F
2.10F
3.1I
3.1 I
2.10 0B
3.B
2.10 F
1.101111I
2.1I
3.I
2.11 1I
3.I
2.11I
1.1011F
*/
//树的结构体+存储数组
struct Node {
int id; //id编号
int left; //左结点
int right; //右结点
char value; //当前结点的value值
} t[N];
int n, m;
string s; //2^n长度的01串
/**
* s
* @param s
* @return
*/
char getStype(string s) {
int zeroCnt = 0, oneCnt = 0;
for (int i = 0; i < s.length(); i++) if (s[i] == '1') oneCnt++; else zeroCnt++;
if (oneCnt == 0)return 'B';
else if (zeroCnt == 0) return 'I';
return 'F';
}
/**
* FBI
* @param start
* @param end
*/
void build(string s, int pos) {
/**
TRS
S1SS_1S_2
S_1RT_1S_2RT_2
*/
char v = getStype(s);
t[pos].id = pos;
t[pos].value = v;
if (s.length() > 1) {
t[pos].left = 2 * pos;
t[pos].right = 2 * pos + 1;
//左树
build(s.substr(0, s.length() / 2), t[pos].left);
//右树
build(s.substr(s.length() / 2), t[pos].right);
}
}
//利用递归进行后序遍历
void post_order(Node node) {
if (node.id) {
post_order(t[node.left]);
post_order(t[node.right]);
cout << node.value;
}
}
int main() {
//这个n是无用的因为是上古的考题都是C时代的要求使用char数组没有n说不过去现在都用string了不需要n了。
cin >> n >> s;
//利用递归构建FBI树
build(s, 1);
//进行后序遍历
post_order(t[1]);
return 0;
}