PAT 甲级 1130 Infix Expression
https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312
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 −. 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 <bits/stdc++.h>
using namespace std; int N;
int vis[30]; struct Node{
string val;
int L, R;
}node[30]; string dfs(int st) {
if(node[st].L == -1 && node[st].R == -1) return node[st].val;
if(node[st].L == -1 && node[st].R != -1) return "(" + node[st].val + dfs(node[st].R) + ")";
if(node[st].L != -1 && node[st].R != -1) return "(" + dfs(node[st].L) + node[st].val + dfs(node[st].R) + ")";
} int main() {
scanf("%d", &N);
for(int i = 1; i <= N; i ++) {
cin >> node[i].val >> node[i].L >> node[i].R;
if(node[i].L != -1) vis[node[i].L] = 1;
if(node[i].R != -1) vis[node[i].R] = 1;
} int root = 1;
while(vis[root] == 1) root ++;
string ans = dfs(root);
if(ans[0] == '(') ans = ans.substr(1, ans.size() - 2);
cout << ans;
return 0;
}
dfs 递归 最后去掉最外面的括号
可能最近是自闭 girl 了 希望有好运气叭
PAT 甲级 1130 Infix Expression的更多相关文章
- PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
- PAT甲级——A1130 Infix Expression【25】
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- 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
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- 1130. Infix Expression (25)
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT甲题题解-1130. Infix Expression (25)-中序遍历
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789828.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 1130 Infix Expression
题意:给出一个语法树(二叉树),输出相应的中缀表达式. 思路:很显然,通过中序遍历来做.通过观察,发现除了根结点之外的所有非叶结点的两侧都要输出括号,故在中序遍历时判断一下即可. 代码: #inclu ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 捕获海康威视IPCamera图像,转成OpenCV能够处理的图像(二)
海康威视IPCamera图像捕获 捕获海康威视IPCamera图像.转成OpenCV能够处理的IplImage图像(一) 捕获海康威视IPCamera图像.转成OpenCV能够处理的IplImage图 ...
- 关于一台机器部署多个tomcat的小记
一台机器部署多个tomcat在很多时候都是有可能的,比如说多个tomcat配合nginx负载更可能好的利用CPU,或者更新程序时做主备切换等. 1.直接下载或者复制一个已有的tomcat,第一个tom ...
- Spring配置跨域请求
本文主要是Spring+SpringMVC+MyBatis/MyBatis Plus框架环境,包括SpringBoot同样适用. 1.编写拦截器 package com.interceptor; im ...
- 可长点心吧-sort
sort #<algorithm> 用的时候一定是 从 第一个(你想要排序的范围内的) 到 最后一个+1 真的错了不止一次了 真的长点心吧
- Photoshop 基础七 位图 矢量图 栅格化
矢量图(CorelDraw)不是像素组成的,放大不会失真,体积小,颜色比较单一.由直线.曲线构成,画一些直线.曲线.多边形.图标. 位图(Photoshop画的就是位图),又像素组成,放大失真,放的越 ...
- DHT11温度传感器应用电路
DHT11应用电路: 注意点: Vcc和GND之间连接一个100 μF的一个电解电容(通常电容的取值在100μF-300μF之间,滤波) SWI接一个5K的上拉电阻,目的是增强驱动能力
- (转)怎么实时查看mysql当前连接数
1.查看当前所有连接的详细资料: ./mysqladmin -uadmin -p123456 -h127.0.0.1 processlist 2.只查看当前连接数(Threads就是连接数.): ./ ...
- session和cookie知识点总结
cookie小结:1.cookie是在服务端创建2.cooki是保存在浏览器这一端3.cookie的生命周期可以通过 cookie.setMaxAge(2000);(如果不设置生命周期,cookie的 ...
- Codeforces Hello 2019
Hello 2019 手速场qwq 反正EGH太神仙了啊.jpg 考试的时候不会啊.jpg A 暴力.jpg #include <cstdio> #include <algorith ...
- 重装系统之无法在驱动器0的分区1上安装windows
在通过U盘或光盘安装win8/win8.1/win10 时,遇到无法安装的问题,提示“无法在驱动器0的分区1上安装windows”,格式化分区也不能解决,进而提示Windows无法安装到这个磁盘,选中 ...