PAT 1130 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<iostream> //海星,建树然后中序遍历
#include<vector>
using namespace std;
struct node{
string val;
node* left;
node* right;
node(string v):val(v), left(NULL), right(NULL){
}
};
vector<int> lc, rc;
vector<string> data;
node* buildtree(node* root, int r){
root=new node(data[r]);
if(lc[r]!=-1)
root->left=buildtree(root->left, lc[r]);
if(rc[r]!=-1)
root->right=buildtree(root->right, rc[r]);
return root;
}
void solution(node* root, int flag){
if((root->left||root->right)&&flag)
cout<<"(";
if(root->left)
solution(root->left, 1);
cout<<root->val;
if(root->right)
solution(root->right, 1);
if((root->left||root->right)&&flag)
cout<<")";
}
int main(){
int n;
cin>>n;
vector<int> visited(n+1, 0);
lc.resize(n+1), rc.resize(n+1);
data.resize(n+1);
for(int i=1; i<=n; i++){
int l, r;
cin>>data[i]>>l>>r;
lc[i]=l;
rc[i]=r;
if(l!=-1) visited[l]=1;
if(r!=-1) visited[r]=1;
}
int i;
for(i=1; i<=n; i++)
if(visited[i]==0)
break;
node* root=NULL;
root=buildtree(root ,i);
solution(root, 0);
return 0;
}
PAT 1130 Infix Expression的更多相关文章
- 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) 时间限制 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 甲级 1130 Infix Expression
https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...
- PAT A1130 Infix Expression (25 分)——中序遍历
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 ...
- PAT1130:Infix Expression
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
随机推荐
- Java中wait和sleep方法的区别
1.两者的区别 这两个方法来自不同的类分别是Thread和Object 最主要是sleep方法没有释放锁,而wait方法释放了锁,使得其他线程可以使用同步控制块或者方法(锁代码块和方法锁). wait ...
- E20170606-hm
pipeline n. 管道; 输油管道; 渠道,传递途径; dump vt. 倾倒; 倾销; 丢下,卸下; 摆脱,扔弃; n. 垃圾场; 仓库; 无秩序地累积;
- 0626-TP整理二(调试模式,空操作,跨控制器调用,跨方法跳转--redirect(),框架语法,创建model模型)
一.调试模式(入口文件:index.php) define('APP_DEBUG', true); //调试模式 define('APP_DEBUG', FALSE); //运行模式 开启日志信息 ...
- bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛【二分+贪心】
二分答案,贪心判定 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...
- zepto中给不存在的元素设置样式并绑定事件的坑
在移动端使用zepto选择器时,一般如果元素不存在会返回一个空的zepto对象. zepto在设置元素样式时,提供了两个入参方式,一种键值对方式$(".ter").css({&qu ...
- [ POI 2012 ] Letters
\(\\\) \(Description\) 给出两个长度为 \(N\) 的字符串\(S_1,S_2\),且保证两个字符串中每一个字符出现次数相同. 现在一次操作可以交换相邻的两个字符,问将 \(S_ ...
- Spartan6系列之GTP Transceiver的介绍与使用
1. 什么是GTP transceiver? GTP transceiver是FPGA里一种线速度达500Mb/sà6.6Gb/s的收发器,利用FPGA内部可编程资源可对其进行灵活地配置, ...
- npm换淘宝源 yarn换淘宝源
查询初始的源 npm get registry > https://registry.npmjs.org/ 设置淘宝源 npm config set registry http://regist ...
- Linux命令运行监测和软件安装
监测命令的运行时间 time command $ time sleep 5 real 0m5.003s # 程序开始至结束的时间,包括其它进程占用的时间片和IO时间 user 0m0.001s # 进 ...
- 初步认识MVC
一丶路由(One) 自定义路由,静态路由,动态路由,组合路由 routes.MapRoute 二丶Action向View传值的四种方式(ViewData.ViewBag.TempData.Model ...