PAT甲级 1130. Infix Expression (25)
来源:互联网 发布:知图科技改成什么了 编辑:程序博客网 时间:2024/06/05 19:47
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<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<string>#include<queue>#include<stack>#include<map>#include<set>using namespace std;#define LL long longconst 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)
- PAT 甲级 1130. Infix Expression (25)
- PAT 1130. Infix Expression (25)
- PAT 1130. Infix Expression (25)
- 【PAT】【Advanced Level】1130. Infix Expression (25)
- PAT A1130. 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)(树的中序遍历)
- PAT 1130. Infix Expression (25) 折腾表达式,未解之谜
- PAT 1130. Infix Expression (25) 前缀表达式、树的中序遍历
- NSProxy——少见却神奇的类
- linux下 zip解压 tar解压 gz解压 bz2等各种解压文件命令
- iOS10权限配置问题及上传Itunes审核注意事项
- 通过 free 命令理解linux 内存管理
- 运维要背锅了!因数据库配置不当美国2亿选民数据泄漏
- PAT甲级 1130. Infix Expression (25)
- 程序员面试金典:字符串--确定两串乱序同构、基本字符串压缩
- 全开源方案实现基于Docker的CI/CD流水线
- Socket通信原理和基础实践
- LeetCode-108. Convert Sorted Array to Binary Search Tree(Java)
- HDU 1166 敌兵布阵(线段树查询+单点更新)
- Android 获取外置SDCard USB等设备
- Trie树
- Android