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 <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct tree
{
char str[];
int l,r;
}s[];
int n,v[],root;
void infix(int k,int flag)
{
if(flag&&s[k].r != -)putchar('(');
if(s[k].l != -)
{
infix(s[k].l,);
}
if(s[k].str[] == '-' && strlen(s[k].str) > )cout<<'('<<s[k].str<<')';
else cout<<s[k].str;
if(s[k].r != -)
{
infix(s[k].r,);
if(flag)putchar(')');
}
}
int main()
{
cin>>n;
for(int i = ;i <= n;i ++)
{
cin>>s[i].str>>s[i].l>>s[i].r;
v[s[i].l] = ;
v[s[i].r] = ;
}
for(int i = ;i <= n;i ++)
{
if(!v[i])
{
root = i;
break;
}
}
if(root != -)infix(root,);
}
1130. Infix Expression (25)的更多相关文章
- PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- PAT甲题题解-1130. Infix Expression (25)-中序遍历
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789828.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...
- PAT 1130 Infix Expression
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- 1130 Infix Expression
题意:给出一个语法树(二叉树),输出相应的中缀表达式. 思路:很显然,通过中序遍历来做.通过观察,发现除了根结点之外的所有非叶结点的两侧都要输出括号,故在中序遍历时判断一下即可. 代码: #inclu ...
- PAT1130:Infix Expression
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- PAT_A1130#Infix Expression
Source: PAT A1130 Infix Expression (25 分) Description: Given a syntax tree (binary), you are suppose ...
随机推荐
- Arthas随笔
目录 Arthas 安装Java 安装 Arthas Arthas 命令及示例 源码分析 Arthas 安装Java 下载jdk 注意 下载的JDK版本要与linux操作系统相匹配,否则汇报No su ...
- Oracle Mysql MSSql 三种数据库 随机查询 条 语句
1. Oracle,随机查询查询语句-20条 select * from ( select * from 表名 order by dbms_random.value ) where rownum ...
- VC程序禁用提示框
程序需要24小时不中断 如果错误提示了的话 runtime error 监控程序就不能重启 下面是网上找的 方便以后用到 http://blog.csdn.net/yuzhiyuxia/article ...
- Docker追加容器端口映射
参考文章: https://blog.csdn.net/chouzhou9701/article/details/86725203
- Oracle 无备份情况下的恢复--控制文件/数据文件
13.3无备份恢复控制文件 没有备份恢复控制文件其实就是在nomount状态,create control创建一个新的控制文件. dba必须知道4个信息才能正确的创建:数据库名.在线日志路径及其大小. ...
- oracle data guard --理论知识回顾02
继上一篇 管理影响物理standby的事件 1 创建表空间或数据文件初始化参数standby_file_management用来控制是否自动将primary数据库增加表空间或数据文件的改动,传播到st ...
- 【ABAP系列】SAP ABAP 运算符
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 运算符 前 ...
- 获取win10壁纸
执行命令会将所有壁纸拷贝到桌面上的wallpaper文件夹内 bat xcopy %LOCALAPPDATA%\Packages\Microsoft.Windows.ContentDeliveryMa ...
- 强化学习之MDP
前言 最近又入坑RL了,要搞AutoML就要学会RL,真的是心累.. 正文 MDP里面比较重要的就是状态值函数和动作-状态值函数吧,然后再求最优状态值函数和最优动作状态值函数,状态值函数的公式推导一开 ...
- 【Linux开发】arm-linux-gnueabihf-gcc下载
原文地址:http://www.veryarm.com/arm-linux-gnueabihf-gcc veryarm是个不错的网站,里面介绍了很多相关的基础知识. arm-linux-gnueabi ...