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 ...
随机推荐
- Android开发之自定义的ProgressDialog
package com.example.dialog; import android.app.ProgressDialog; import android.content.Context; /** * ...
- andorid 进度条和图片的透明度
layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- 卸载服务器GitLab
sudo gitlab-ctl uninstall sudo rpm -e gitlab-ce find / -name gitlab|xargs rm -rf
- NC 6系预警类型注册
在实际开发预警任务中,因为模块是新创建的,所以开发预警,就要在相应的节点模块注册.但这样代码就得放在相应的模块中,注册个预警类型,就可以把代码直接放在自己新建的模块. .先执行新建模块语句 inser ...
- UI设计教程分享:PS故障风海报制作教程
1.首先找一张看起来很酷的图(也可以选择自己喜欢的图片): 2. 复制图层,点击添加图层样式,选择混合选项,在高级混合里面的通道选项,有R.G.B三个通道选项,默认是全部勾选的状态,选择其中一个勾 ...
- django添加装饰器
引入模块: from django.utils.decorators import method_decorator 添加:@method_decorator(func) from django.ut ...
- 如何利用Photoshop进行快速切图
在UI设计中我们常常使用Ai来进行矢量图的绘制,然后导入Ps中进行设计.排版和导出. 在以前的版本中,切图一直是个很麻烦的事情,要么依托于脚本,要么手动一张张导出,很不方便,这种窘况在Photosho ...
- python线程池
https://blog.csdn.net/qq_33961117/article/details/82587873#!/usr/bin/python # -*- coding: utf- -*- f ...
- Max Chunks To Make Sorted LT769
Given an array arr that is a permutation of [0, 1, ..., arr.length - 1], we split the array into som ...
- PHP 写文件的例子
$contents = "All the content"; $dir = 'c:'; $file_path = $dir . "\\content.txt"; ...