Comments (4)
我在您的代码上简单修改了一下。
添加了key作为索引。前中后序中均保存索引值。然后用value存储具体的值。
最后ac代码如下:
#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;
}
from pat.
另外,其实也可以最后不保存postorder的,在post.push_back(pre[root]);的时候,直接输出即可。
设置一个flag,用于标记是否是第一个数。
也就是说,可以写成
//之前设置bool flag=false;
if (flag)
cout << ' ';
cout << value[pre[root]];
flag = true;
from pat.
已修改为您的代码并署名,感谢~
from pat.
我又被翻牌了,开熏。
from pat.
Related Issues (20)
- [Advanced/C++/1018] Suggestion
- Basic level C++ 1009说反话 HOT 1
- License
- 1018 不必模拟整个过程,need记录权重最小值即可
- [Advanced/C++/1135] Suggestion
- [Advanced/C++/1095] bug report
- [Advanced/C++/1159] Suggestion
- [Advanced/C++/1103]代码执行结果与样本输出结果不同,但是能够AC
- [Basic level Java] 1015 德才论
- [Basic level Java] 1017. A除以B (20) 全部运行超时
- PAT (Basic Level)1008 数组元素循环右移问题 更为简单做法
- 【改写原解法】PAT【甲级】1060 Are They Equal (25 分)
- 【甲级1014另解】1014 Waiting in Line
- PAT BasicLevel 1032
- Lanqiao
- 乙级1056 组合数的和(C++)更优解
- [Advence/c++/1026]测试样例改变,导致第一个测试点过不去
- 提供乙级1008 python版本的PythonIC风格代码
- Pat乙级1008 数组元素循环右移问题(简洁到极致的写法)
- PAT-B 1018. 锤子剪刀布 (20)(最简洁巧妙的写法)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pat.