题解:暴力模拟过的,debug了好久,结果一看题解我哭了
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x * f;
}
int n, sum = 1;
struct node {
int l, r;
int val;
} vis[100000];
char p1[1050];
int cnt, p[1050];
inline int tree(int l, int r) {
if (l > r) {
return 0;
}
cnt++;
if (l == r) {
vis[cnt].val = p[l] + 1;
return cnt;
}
int now = cnt;
int mid = (l + r) / 2;
vis[now].l = tree(l, mid);
vis[now].r = tree(mid + 1, r);
if (vis[now].l && vis[now].r) {
if (vis[vis[now].l].val == 1 && vis[vis[now].r].val == 1)
vis[now].val = 1; //全0
else if (vis[vis[now].l].val == 2 && vis[vis[now].r].val == 2)
vis[now].val = 2; //全1
else if (vis[vis[now].l].val == 3 && vis[vis[now].r].val == 3)
vis[now].val = 3; //有0有1
else if (vis[vis[now].l].val + vis[vis[now].r].val >= 3)
vis[now].val = 3; //有0有1
} else {
if (vis[vis[now].l].val + vis[vis[now].r].val == 1)
vis[now].val = 1; //全0
else if (vis[vis[now].l].val + vis[vis[now].r].val == 2)
vis[now].val = 2; //全1
else if (vis[vis[now].l].val + vis[vis[now].r].val == 3)
vis[now].val = 3; //有0有1
}
return now;
}
inline void pintf(int x) {
if (vis[x].l) {
pintf(vis[x].l);
}
if (vis[x].r) {
pintf(vis[x].r);
}
if (vis[x].val == 1) {
cout << "B";
} else if (vis[x].val == 2) {
cout << "I";
} else if (vis[x].val == 3) {
cout << "F";
}
}
int main() {
n = read();
for (int i = 0; i < n; ++i) {
sum *= 2;
}
cin >> p1;
for (int i = 0; i < sum; ++i) {
p[i] = p1[i] - '0';
}
tree(0, sum - 1);
pintf(1);
return 0;
}