-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PAT-A-1086 报告Bug #45
Comments
我在您的代码上简单修改了一下。 #include <cstdio>
#include <vector>
#include <stack>
#include <cstring>
using namespace std;
vector<int> pre, in, post,value;
void postorder(int root, int start, int end) {
if (start > end) return;
int i = start;
while (i < end && in[i] != pre[root]) i++;
postorder(root + 1, start, i - 1);
postorder(root + 1 + i - start, i + 1, end);
post.push_back(pre[root]);
}
int main() {
int n;
scanf("%d", &n);
char str[5];
stack<int> s;
int key=0;
while (~scanf("%s", str)) {
if (strlen(str) == 4) {
int num;
scanf("%d", &num);
value.push_back(num);
pre.push_back(key);
s.push(key++);
}
else {
in.push_back(s.top());
s.pop();
}
}
postorder(0, 0, n - 1);
printf("%d", value[post[0]]);
for (int i = 1; i < n; i++)
printf(" %d",value[post[i]]);
return 0;
} |
另外,其实也可以最后不保存postorder的,在post.push_back(pre[root]);的时候,直接输出即可。 //之前设置bool flag=false;
if (flag)
cout << ' ';
cout << value[pre[root]];
flag = true; |
已修改为您的代码并署名,感谢~ |
我又被翻牌了,开熏。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
您好:
在A-1086题中,题目并没有说所有节点的值互不相同。因此,在有多个节点的值相同的情况下,您的代码会输出错误的结果。
虽然一开始,我也是这么写的 : P
例如,测试数据:
The text was updated successfully, but these errors were encountered: