7-3 Postfix Expression (25 分)
Given a syntax tree (binary), you are supposed to output the corresponding postfix expression, with parentheses reflecting the precedences of the operators.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 20) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the i-th line corresponds to the i-th node) in the format:
data left_child right_child
where data
is a string of no more than 10 characters, left_child
and right_child
are the indices of this node's left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by −1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.
Figure 1 | Figure 2 |
Output Specification:
For each case, print in a line the postfix expression, with parentheses reflecting the precedences of the operators.There must be no space between any symbols.
Sample Input 1:
8
* 8 7
a -1 -1
* 4 1
+ 2 5
b -1 -1
d -1 -1
- -1 6
c -1 -1
Sample Output 1:
(((a)(b)+)((c)(-(d))*)*)
Sample Input 2:
8
2.35 -1 -1
* 6 1
- -1 4
% 7 8
+ 2 3
a -1 -1
str -1 -1
871 -1 -1
Sample Output 2:
(((a)(2.35)*)(-((str)(871)%))+)
- 题解:代码写了45分钟,过了样例,代码里面其实该记的都是要记的,但是考场上还是有一部分的内容需要你去临场去设计的,所以其实这个阶段主要是适应这个编写的时间和对一些常见的模板进行整理的过程。。。前面几天把PAT超市里面所有需要收费的模拟卷都做了一轮,因为3个小时的限时提交时间,连着不喘气做了3个小时人都快没了,就没有心情和精力去写博客了。。。现在的话是把这些模拟卷的题目再做一遍,查缺补漏一轮。。。。,等这一轮做完,感觉差不都就是考试了。。。,PAT的备考过程确实对我的考研上面的时间规划和心态的调整适应上面给到了非常大的帮助。。。。。前面2月26号,四六级出成绩,我2020年12月份的六级考了546,9月份的四级考了596.。。。其实我去考英语也不是为了以后找工作的证书,主要是为了准备考研英语来摸一个底的。。。我英语基本上是裸考,都是考平常看美剧和英语的演讲啊,科普视频啊之类的边玩边学些的。。。专门复习的话,作文应该会更高。。。我希望研究生英语考80分以上。。。
- 我觉得考研考研,说穿了,就是拼几样东西,一个是你多想念研究生。。。,这个决定了你会付出多少的努力,还有一个是你的学习方法,这个决定了你付出努力的效率,做的是“有用功”还是“无用功”。。。最后一个是你的心态的调整,你的考研的认识,你对硕士文凭的认识,你对未来的期许,你如何处理人际关系。。。这个太重要了。。。。我并不是说除了研究生以外就“无路可走”,我只是说,经过这场考试的备考是一场人生的历练,和大学一样,你最后学到的并不只是知识
- 我现在觉得一句话说的很有道理,当你为了备战考试一路走来付出了许多许多,你竭尽全力去做,到最后,这场考试的结果已经并不重要,这并不是一种安慰的话语,而是一种最最真实的感受
-
// 7-3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <bits/stdc++.h> using namespace std; struct node { string val; node* left; node* right; int left_num; int right_num; node() { left = right = NULL; } }; vector<node> seq; unordered_map<int, bool> parent; void post_order(node* root) { if (root->val == "-") { cout << "("; if (root->left != NULL) { post_order(root->left); } cout << root->val; if (root->right != NULL) { post_order(root->right); } cout<< ")"; } else { cout << "("; if (root->left != NULL) { post_order(root->left); } if (root->right != NULL) { post_order(root->right); } cout << root->val; cout << ")"; } } int main() { #ifndef ONLINE_JUDGE FILE* s; freopen_s(&s, "in.txt", "r", stdin); #endif // !ONLINE_JUDGE int n; cin >> n; string data; int left_child, right_child; for (int i = 0; i < n; i++) { cin >> data >> left_child >> right_child; node* temp = new node(); temp->val = data; temp->left_num = left_child; temp->right_num = right_child; seq.push_back(*temp); } for (int i = 0; i < n; i++) { if (seq[i].left_num != -1) { seq[i].left = &seq[seq[i].left_num - 1]; parent[seq[i].left_num - 1] = true; } if (seq[i].right_num != -1) { seq[i].right = &seq[seq[i].right_num - 1]; parent[seq[i].right_num - 1] = true; } } int q; for (q = 0; q < n; q++) { if (parent[q] != true) { break; } } node* root = &seq[q]; post_order(root); cout << endl; return 0; }