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 ...
随机推荐
- DevExpress之TreeList节点绑定图片
最近在项目中使用到了DX中的TreeList控件绑定数据源时在每个节点前显示特点的图片:查阅相关资料实现方法如下:1.首先打开VS2010新建一个WINFROM应用程序: 2.在WINFROM应用程序 ...
- RNA测序相对基因表达芯片有什么优势?
RNA测序相对基因表达芯片有什么优势? RNA-Seq和基因表达芯片相比,哪种方法更有优势?关键看适用不适用.那么RNA-Seq适用哪些研究方向?是否您的研究?来跟随本文了解一下RNA测序相对基因表达 ...
- Binary Prefix Divisible By 5 LT1018
Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a bi ...
- Socket 学习笔记 01 常用函数
常用方法 创建套接字: socket() 绑定本机端口: bind() 建立连接: connect(),accept() 侦听端口: listen() 数据传输: send() ...
- JVM新生代到老年代基础了解
JVM区域总体分两类,heap区和非heap区. heap区又分为: - Eden Space(伊甸园). - Survivor Space(幸存者区). - Old Gen(老年代). 1个Eden ...
- springMVC学习一 工作机制
springMVC下面的四大组件: (1)DispatcherServlet : 前端控制器,接收所有请求 ,并把请求路径和请求参数解析出来,本质是一个servlet在web.xml中配置 (如果配置 ...
- 【转】Linux 如何通过命令仅获取IP地址
一同事的朋友正在参加笔试,遇到这么一个问题让他帮忙解决,结果同事又找到我帮他搞定.真是感慨:通讯发达在某些方面来说,真不知是不是好事啊!题目大致如下所示,一般我们使用ifconfig查看网卡信息,请问 ...
- oracle merge into语法
oracle的merge into语法,在这种情况下: 基于某些字段,存在就更新,不存在就插入 不需要先去判断一下记录是否存在,直接使用merge into oerge into 语法: MERGE ...
- java 模拟登录新浪微博(通过cookie)
这几天一直在研究新浪微博的爬虫,发现爬取微博的数据首先要登录.本来打算是通过账号和密码模拟浏览器登录.但是现在微博的登录机制比较复杂.通过账号密码还没有登录成功QAQ.所以就先记录下,通过cookie ...
- 2018.11.02 洛谷P3952 时间复杂度(模拟)
传送门 惊叹考场dubuffdubuffdubuff. 这题还没有梭哈难啊233. 直接按照题意模拟就行了. 代码: #include<bits/stdc++.h> using names ...