PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25)
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<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define LL long long
const int inf=0x3f3f3f3f; struct node
{
string s;
int pre,l,r;
} tree[105]; int findroot(int pos)
{
if(tree[pos].pre==-1)
return pos;
return findroot(tree[pos].pre);
} void dfs(int pos,int deep)
{
if(pos==-1)
return;
if(deep!=0&&(tree[pos].l!=-1||tree[pos].r!=-1))
printf("(");
dfs(tree[pos].l,deep+1);
cout<<tree[pos].s;
dfs(tree[pos].r,deep+1);
if(deep!=0&&(tree[pos].l!=-1||tree[pos].r!=-1))
printf(")");
}
int main()
{
string s;
int l,r,n;
scanf("%d",&n);
for(int i=0; i<100; i++)
tree[i].l=tree[i].r=tree[i].pre=-1;
for(int i=1; i<=n; i++)
{
cin>>tree[i].s>>tree[i].l>>tree[i].r;
tree[tree[i].l].pre=tree[tree[i].r].pre=i;
}
int root=findroot(1);
dfs(root,0);
return 0;
}
PAT甲级 1130. Infix Expression (25)的更多相关文章
- 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特别不喜欢那些随便转载别人的原创文章又不给 ...
- 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 ...
- 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)
题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...
- PAT甲级 1122. Hamiltonian Cycle (25)
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
随机推荐
- POJ 3469.Dual Core CPU 最大流dinic算法模板
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 24830 Accepted: 10756 ...
- PHD实时数据对象
PHD实时数据库在化工制造业的应用 PISDK 开发包 电力企业信息化
- 什么时候出现死锁,如何解决?mysql 引擎? 多个like or 查询sql如何优化?什么是常量池?for条件执行顺序
1. 什么时候出现死锁,如何解决?mysql 引擎? 多个like or 查询sql如何优化? 资源竞争导致死锁,比如A B 同时操作两条记录,并等待对方释放锁. 优化sql, 加缓存,主从(如读写分 ...
- JS在页面根据数量改变总价及按钮进行格式验证
分两部分,第一部分是在页面上,根据数量的input标签内容,动态更新总价标签的内容,代码如下: <script type="text/javascript"> $(do ...
- git学习笔记:一台电脑上配置两个git账户
如何在一台电脑上配置两个git账户,现在云端仓库很多,有开源中国的 gitee.com 微软的 github.com 还有 gitlab.com 和 bitbucket.org 等等,下面是具体步骤 ...
- JS Async Callback
AsyncCallback 意义: 异步操作完成时调用的方法 语法1: 构造异步回调对象 AsyncCallback 异步回调对象名asyncCallback = new AsyncCallback( ...
- GC收集器种类
转载:https://wangkang007.gitbooks.io/jvm/content/la_ji_shou_ji_qi.html 收集器 1.1 Serial(串行)收集器 Serial收集器 ...
- ServiceDesk Plus 服务管理自动指派工单功能
- 使用delphi-cross-socket 开发kbmmw smart http service
前几天我说了使用delphi-cross-socket 扩展kbmmw 的跨平台支持,今天我说一下使用 kbmMWCrossScoketHttpServerTransport 在linux 下支持 k ...
- Tkinter添加图片
Tkinter添加图片的方式,与Java相似都是利用label标签来完成的: 一.默认的是gif的格式,注意将png后缀名修改为gif还是无法使用,文件格式依然错误. photo = PhotoIma ...