A1130. Infix Expression
Given a syntax tree (binary), you are supposed to output the corresponding infix 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 infix expression, with parentheses reflecting the precedences of the operators. Note that there must be no extra parentheses for the final expression, as is shown by the samples. 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))
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef struct{
int lchild, rchild;
string data;
}node;
node tree[];
int N, notRoot[] = {};
void inOrder(int root, int first){
if(root == -)
return;
string ss = tree[root].data;
if(tree[root].rchild != - && first == )
cout << "(";
inOrder(tree[root].lchild, );
cout << ss;
inOrder(tree[root].rchild, );
if(tree[root].rchild != - && first == )
cout << ")";
}
int main(){
scanf("%d", &N);
for(int i = ; i <= N; i++){
int cl, cr;
string ss;
cin >> ss >> cl >> cr;
tree[i].lchild = cl;
tree[i].rchild = cr;
tree[i].data = ss;
if(cl != -)
notRoot[cl] = ;
if(cr != -)
notRoot[cr] = ;
}
int root;
for(root = ; root <= N && notRoot[root] == ; root++);
inOrder(root, );
cin >> N;
return ;
}
总结:
1、中序遍历即可,当该节点不是叶节点时,则需要先输出" ( ", 再递归访问左子树,右括号同理。起初我是用判断 + - * %来判断是否是非叶节点,结果最后一个测试点过不去。后来改成判断子树是否为空就全过了。这里可能是由于运算符号不止有+-*%,可能还有未知的运算符号。
A1130. Infix Expression的更多相关文章
- PAT A1130 Infix Expression (25 分)——中序遍历
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT甲级——A1130 Infix Expression【25】
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT_A1130#Infix Expression
Source: PAT A1130 Infix Expression (25 分) Description: Given a syntax tree (binary), you are suppose ...
- PAT1130:Infix Expression
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- PAT 甲级 1130 Infix Expression
https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...
- PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- PAT 1130 Infix Expression[难][dfs]
1130 Infix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspond ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
- PAT 1130 Infix Expression
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
随机推荐
- 剑指offer(11)
题目: 输入一个链表,输出该链表中倒数第k个结点. 思路: 我们一先想到的应该是循环两次链表,第一次获得它的长度,然后用长度-k,得出目标节点在链表的第几位,再循环一次. 如果要求只用一次循环的话,我 ...
- qtp 自动化测试桌面程序-点滴1(录制设置、共用文件)
1 automation-record and run settings--设置录制程序 2 将function/repository 放于单独于test的文件夹中-方便多个test使用同一个仓库.函 ...
- phonegap-plugin-contentsync
一.API 1.ContentSync.sync(options) options.src : 字符串类型 (必选项)远程托管内容的URL.更新一个生产环境下的APP,常使用HTTPS option ...
- oracle数据库备份和恢复
参考地址:https://www.cnblogs.com/1175429393wljblog/p/9529334.html Oracle数据导入导出imp/exp 在cmd的dos命令提示符下执行,而 ...
- codeforces474D
Flowers CodeForces - 474D 话说某个幸运的小伙伴X拿到了kevin女神送的蛋糕,然而他的吃法非常奇特,他独创了两种吃蛋糕的办法:一.一次吃一整个蛋糕:二.一次吃k个蛋糕. 那么 ...
- ubuntu系统安装mysql(deb-bundle包)
由于某些原因,又要在ubuntu系统中安装mysql了,之前曾经安装过好多次.都没记下来 以前一直动用源码包来安装,基于两个原因:1.一直用Python写代码.2.想使用文件来安装,而不是通过api ...
- 洛谷P1622释放囚犯
题目: 这个题很明显是一个区间DP,但是比较不同的是,这个题它很像区间DP的经典题——石子合并. 然后我傻傻的搞了这个题搞了一下午,然后几乎看遍了全网的题解,就只看懂了这个方法,可能是我太菜了吧,但是 ...
- [SCOI2005] 互不侵犯
传送门:>Here< 解题思路 其实这道题一种很简单的解法是搜索+打表,但是这样很赖皮.这里给出一种状压DP的解法. 很显然利用普通的DP无法解决了,因为针对点来转移是很难的.但看到$N& ...
- python学习日记(流程控制习题)
请输出1-2+3...+99除88以外的和 i = 1 sum = 0 while i <= 99: if i == 88: i = i + 1 continue else: if i%2 == ...
- Linux 日志分析脚本
#### 以下代码,若出现无法使用,请根据底下图片,更改参数.根据 apache 日志格式修改 查看 apache 进程ps aux | grep httpd | grep -v grep | wc ...