当前位置:网站首页>Pat class a 1162 postfix expression
Pat class a 1162 postfix expression
2022-07-05 02:46:00 【Handsome BIA】
1162 Postfix Expression (25 branch )
Given a syntax tree (binary), you are supposed to output the corresponding postfix 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 postfix expression, with parentheses reflecting the precedences of the operators.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)%))+)
Thought analysis : This topic and 1130 similar , Are given an expression tree to find his expression , This topic requires finding the suffix expression , Then the idea is very clear , We just need to build a tree , Then it is OK to print after traversal , But pay attention to , If there is a node without a left child , Then we have to print the root node first , Here we use a st Array tags are ok . One last word , Input methods like this , We use two-dimensional arrays tree[N][2] Building trees can be more convenient , The second is to store the location of the two children , Use another w[N] Stored value
#include<bits/stdc++.h>
using namespace std;
const int N = 25;
int tree[N][2];
string w[N],value;
bool st[N];
int n,l,r,root;
void print(int t)
{
if(!st[t]) cout<<w[t];
st[t] = 1;
}
void dfs(int t)
{
if (t == -1) return ;
printf("(");
if(tree[t][0] == -1) print(t);
dfs(tree[t][0]);
dfs(tree[t][1]);
print(t);
printf(")");
}
int main()
{
cin >> n;
for (int i = 1 ;i <= n ; i ++ )
{
cin >> value >> l >> r;
tree[i][0] = l,tree[i][1] = r;
st[l] = 1,st[r] = 1;
w[i] = value;
}
for (int i = 1; i <= n; i ++ ) if(!st[i]) root = i;
memset(st,0,sizeof st);
dfs(root);
return 0;
}
There is probably so much content , I hope I can help you .
边栏推荐
- Yuan universe also "real estate"? Multiple second-hand trading websites block metauniverse keywords
- Erreur de type de datagramme MySQL en utilisant Druid
- 看 TDengine 社区英雄线上发布会,听 TD Hero 聊开发者传奇故事
- Why is this an undefined behavior- Why is this an undefined behavior?
- Apache build web host
- Idea inheritance relationship
- Avoid material "minefields"! Play with super high conversion rate
- Openresty ngx Lua Execution stage
- Blue bridge - maximum common divisor and minimum common multiple
- Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
猜你喜欢
Utilisation simple de devtools
Devtools的簡單使用
Port, domain name, protocol.
Why are there fewer and fewer good products produced by big Internet companies such as Tencent and Alibaba?
this+闭包+作用域 面试题
Pytest (5) - assertion
丸子百度小程序详细配置教程,审核通过。
Qrcode: generate QR code from text
Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
Simple use of devtools
随机推荐
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
openresty ngx_lua執行階段
[uc/os-iii] chapter 1.2.3.4 understanding RTOS
spoon插入更新oracle数据库,插了一部分提示报错Assertion botch: negative time
Azkaban概述
Naacl 2021 | contrastive learning sweeping text clustering task
Summary and practice of knowledge map construction technology
[200 opencv routines] 99 Modified alpha mean filter
CAM Pytorch
Comparison of advantages and disadvantages between platform entry and independent deployment
[Yu Yue education] National Open University autumn 2018 8109-22t (1) monetary and banking reference questions
Use UDP to send a JPEG image, and UPD will convert it into the mat format of OpenCV after receiving it
When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 1)
Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Sqoop命令
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Acwing第 58 场周赛【完结】
Last week's hot review (2.7-2.13)