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)的更多相关文章

  1. PAT甲级 1130. Infix Expression (25)

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

  2. PAT甲题题解-1130. Infix Expression (25)-中序遍历

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789828.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  3. PAT甲级——1130 Infix Expression (25 分)

    1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...

  4. PAT 1130 Infix Expression[难][dfs]

    1130 Infix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspond ...

  5. PAT 甲级 1130 Infix Expression

    https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...

  6. PAT 1130 Infix Expression

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  7. 1130 Infix Expression

    题意:给出一个语法树(二叉树),输出相应的中缀表达式. 思路:很显然,通过中序遍历来做.通过观察,发现除了根结点之外的所有非叶结点的两侧都要输出括号,故在中序遍历时判断一下即可. 代码: #inclu ...

  8. PAT1130:Infix Expression

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

  9. PAT_A1130#Infix Expression

    Source: PAT A1130 Infix Expression (25 分) Description: Given a syntax tree (binary), you are suppose ...

随机推荐

  1. 《Effective Java》读书笔记 - 6.枚举和注解

    Chapter 6 Enums and Annotations Item 30: Use enums instead of int constants Enum类型无非也是个普通的class,所以你可 ...

  2. code first System.Data.Entity.Infrastructure.CommitFailedException: An error was reported while committing a database transaction but it could not be determined whether the transaction succeeded

    System.Data.Entity.Infrastructure.CommitFailedException: An error was reported while committing a da ...

  3. @清晰掉 C++ 中的 enum 结构在内存中是怎么存储的?

     C++ 中的 enum 结构在内存中是怎么存储的? C++ C++ 中的 enum 结构在内存中是怎么存储的?里面存储的是常量值吗?   关于占用内存的大小,enum类型本身是不占内存的,编译器直接 ...

  4. C++获取寄存器eip的值

    程序中需要打印当前代码段位置 如下 #include <stdio.h> #include <stdlib.h> #include <math.h> #ifdef ...

  5. Nginx+Php不支持并发,导致curl请求卡死(Window环境)

    1.问题描述:项目中开发很多对外接口,于是在本项目中写了测试脚本来验证接口.然鹅,发现Curl请求出现卡死情况,没有响应. 2.具体原因:在window环境下配置的nginx+php环境时,windo ...

  6. 细数EDM营销中存在的两大盲点

    国庆节了,祝大家国庆快乐,转眼博客至今已有三年了.下面博主为大家介绍EDM营销中存在的两大盲点,供大家参考. 一是忽略用户友好.用户友好策略是Email营销成功的关键要素,具体包括内容友好策略.方式友 ...

  7. Octavia 的实现与分析(OpenStack Rocky)

    目录 文章目录 目录 Octavia 基本对象概念 基本使用流程 软件架构 服务进程清单 代码结构 loadbalancer 创建流程分析 network_tasks.AllocateVIP netw ...

  8. 系统分析与设计HW8

    描述软件架构与框架之间的区别与联系 软件架构是有关软件整体结构与组件的抽象描述,用于指导大型软件系统各个方面的设计.架构模式(style)是特定领域常见问题的解决方案. 框架是特定语言和技术的架构应用 ...

  9. Call to undefined method app\models\User::find() yii2-admin

    这个问题可能大家遇到的不多. 分析原因 问题出在 config/web.php 这个配置文件里面 'components' => [ ..... 'user' => [ 'identity ...

  10. sql语句实现行转列的3种方法实例

    sql语句实现行转列的3种方法实例 一般在做数据统计的时候会用到行转列,假如要统计学生的成绩,数据库里查询出来的会是这样的,但这并不能达到想要的效果,所以要在查询的时候做一下处理,下面话不多说了,来一 ...