PAT 1130. Infix Expression (25) 折腾表达式,未解之谜
来源:互联网 发布:java中实例化是什么 编辑:程序博客网 时间:2024/05/17 12:01
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.
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 7a -1 -1* 4 1+ 2 5b -1 -1d -1 -1- -1 6c -1 -1Sample Output 1:
(a+b)*(c*(-d))Sample Input 2:
82.35 -1 -1* 6 1- -1 4% 7 8+ 2 3a -1 -1str -1 -1871 -1 -1Sample Output 2:
(a*2.35)+(-(str%871))
#include <iostream>#include<stdio.h>#include<queue>#include<algorithm>#include<vector>#include<string>#include<string.h>#include<set>#include<deque>#include<queue>using namespace std;struct{ string value; int lc,rc;}node[60];int start;int mark[50]={0};void inorder(int x){ if(x==start) { if(node[x].lc!=-1) inorder(node[x].lc); cout<<node[x].value; if(node[x].rc!=-1) inorder(node[x].rc); return ; } if(node[x].lc==-1&&node[x].rc==-1) { cout<<node[x].value; return; } else { cout<<'('; if(node[x].lc!=-1) inorder(node[x].lc); cout<<node[x].value; if(node[x].rc!=-1) inorder(node[x].rc); cout<<')'; }}int main(){ int n;cin>>n; for(int i=1;i<=n;i++) { string a; int b,c; cin>>a>>b>>c; if(b!=-1) mark[b]=1; if(c!=-1) mark[c]=1; node[i].lc=b; node[i].rc=c; node[i].value=a; } start=1; while(mark[start]==1) start++; inorder(start); return 0;}
- PAT 1130. Infix Expression (25) 折腾表达式,未解之谜
- PAT 1130. Infix Expression (25)
- PAT 1130. Infix Expression (25)
- PAT 1130. Infix Expression (25)-甲级
- PAT甲级 1130. Infix Expression (25)
- 【PAT】【Advanced Level】1130. Infix Expression (25)
- PAT 甲级 1130. Infix Expression (25)
- PAT A1130. Infix Expression (25)
- PAT 1130. Infix Expression (25) 前缀表达式、树的中序遍历
- 1130. Infix Expression (25)
- 1130. Infix Expression (25)
- 1130. Infix Expression (25)
- 1130. Infix Expression (25)
- 1130. Infix Expression (25)
- 1130. Infix Expression (25)
- 1130. Infix Expression (25)
- PAT--1130. Infix Expression(中序加括号)
- PAT(A)-1130. Infix Expression (25)(树的中序遍历)
- win32汇编的segment,子程序,宏定义,局部变量定义test1
- 内存溢出 java.lang.OutOfMemoryError: PermGen space
- C++的一个输出方阵程序
- 并发工具类(一)等待多线程完成的CountDownLatch
- 并发工具类(二)同步屏障CyclicBarrier
- PAT 1130. Infix Expression (25) 折腾表达式,未解之谜
- 2017-09-14 LeetCode_315 Count of Smaller Numbers After Self
- could not extract ResultSet
- 欢迎使用CSDN-markdown编辑器
- 织梦调用缩略图原图的方法
- Nginx web服务器
- 怎么用PHP实现检测网站文章是否被收录
- RHEL6.5 db2_data_server_client客户端 安装与配置
- C/C++内存分配——栈、堆、静态区、符号区等等