这个代码在CLion里可以第二个点正常运行出正确结果,但是在Visual Studio里会无限递归,以及第三个点相同的代码有的时候MLE,有的时候RE,第二个点有的时候会TLE,不知道有没有朋友可以帮忙解答一下出现这些问题的原因。 代码如下:
#include <bits/stdc++.h>
const int maxN = 1024 + 5;
struct TNode {
char s[maxN];
char fbi;
int lChild = -1, rChild = -1;
}Tree[3 * maxN];
int flag;
enum f{FStr, BStr, IStr};
f judgeFBI(TNode T) {
bool flag;
int n = strlen(T.s);
for (int i = 0; i < n; ++i) {
if (i == 0 && T.s[i] == '0') {
flag = false;
} else if (i == 0 && T.s[i] == '1') {
flag = true;
} else {
if (((flag == 0 && T.s[i] == '0') || (flag == 1 && T.s[i] == '1')) && i != n - 1) {
continue;
} else if ((flag == 0 && T.s[i] != '0') || (flag == 1 && T.s[i] != '1')) {
return FStr;
} else if (flag == 0 && T.s[i] == '0' && i == n - 1) {
return BStr;
} else if (flag == 1 && T.s[i] == '1' && i == n - 1) {
return IStr;
} else return FStr;
}
}
}
void createTree(char *str, int len, int de) {
if (strlen(str) == 1) {
strcat(Tree[flag].s, str);
if (Tree[flag].s[0] == '1') Tree[flag].fbi = 'I';
else if (Tree[flag].s[0] == '0') Tree[flag].fbi = 'B';
else Tree[flag].fbi = 'F';
return;
}
int l = len / 2;
char str1[maxN], str2[maxN];
memset(str1, '\0', sizeof(str1));
memset(str2, '\0', sizeof(str2));
strncat(str1, str, l);
strncat(str2, str + l, l);
strcpy(Tree[flag].s, str);
f judge = judgeFBI(Tree[flag]);
if (judge == IStr) Tree[flag].fbi = 'I';
else if (judge == BStr) Tree[flag].fbi = 'B';
else if (judge == FStr) Tree[flag].fbi = 'F';
int tmp = flag;
flag += 1;
Tree[tmp].lChild = flag;
createTree(str1, len / 2, de);
flag += 1;
Tree[tmp].rChild = flag;
createTree(str2, len / 2, de);
}
void postOrder(int loc, int de) {
if (Tree[loc].rChild == -1 && Tree[loc].lChild == -1) {
std::cout << Tree[loc].fbi;
return;
}
postOrder(Tree[loc].lChild, de);
postOrder(Tree[loc].rChild, de);
std::cout << Tree[loc].fbi;
}
int main() {
int n;
std::cin >> n;
char str[maxN];
int len = 1;
for (int i = 0; i < n; ++i) {
len *= 2;
}
char noUse[maxN];
gets(noUse);
for (int i = 0; i < len; ++i) {
str[i] = getchar();
}
flag = 0;
createTree(str, len, len * 2 - 1);
flag = 0;
postOrder(flag, len * 2 - 1);
return 0;
}