PAT(甲级)2019年秋季考试 7-3 Postfix Expression (25 分) 凌宸1642
PAT(甲级)2019年秋季考试 7-3 Postfix Expression (25 分)
题目描述:
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.
译:每个输入文件包含一个测试用例,对于每种情况,第一行包含一个表示语法树中结点总数的整数 N (≤20) 。 接下来 N 行,每行用如下格式给定一个结点的信息(第 i 行 表示第 i 个结点):
data left_child right_child
data
是一个不超过 10 个字母的字符串, left_child
和right_child
是左右孩子结点的索引。结点从 1 到 N 编号。空链 NULL 用 -1 表示。
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 Input1 (样例输入1):
8
* 8 7
a -1 -1
* 4 1
+ 2 5
b -1 -1
d -1 -1
- -1 6
c -1 -1
Sample Output1 (样例输出1):
(((a)(b)+)((c)(-(d))*)*)
Sample Input2 (样例输入2):
8
2.35 -1 -1
* 6 1
- -1 4
% 7 8
+ 2 3
a -1 -1
str -1 -1
871 -1 -1
Sample Output2 (样例输出2):
(((a)(2.35)*)(-((str)(871)%))+)
The Idea:
乍一看好像就是一个后序遍历,后缀表达式的求解。
但是当你写好后缀表达式之后,你会发现,得到的答案与给出的两个样例都有差别。
仔细研究样例,可发现,样例中的 -
作为负号
时,其访问顺序并不是后缀,而是一种中缀,当其不是算符的时候,会满足左子树为空,此时的正确的语法表达为 先访问根节点,再访问右子树。
其他时候都是先访问左子树,再访问右子树,再访问根节点。
The Codes:
#include<bits/stdc++.h>
using namespace std ;
struct node{
string val ;
int left = -1 , right = -1 ;
}tree[22] ;
int n , fa[22] ;
string ans ;
string dfs(int root){
if(root == -1) return "" ; // 空节点
// 叶子结点,直接返回字符串: "(tree[root].val)"
if(tree[root].left == -1 && tree[root].right == -1) return "(" +tree[root].val+ ")";
string s = "(" ;
if(tree[root].left == -1 && tree[root].right != -1){ // 左子树为空,右子树非空
s += tree[root].val ;
s += dfs(tree[root].right) ;
}else{
s += dfs(tree[root].left) ;
s += dfs(tree[root].right) ;
s += tree[root].val ;
}
s += ")" ;
return s ;
}
int main(){
for(int i = 1 ; i < 22 ; i ++) fa[i] = i ; // 初始化
scanf("%d" , &n) ;
int l , r ;
for(int i = 1 ; i <= n ; i ++){
cin >> tree[i].val >> l >> r ;
if(l != -1) {
fa[l] = i ;
tree[i].left = l ;
}
if(r != -1) {
fa[r] = i ;
tree[i].right = r ;
}
}
int root ;
for(int i = 1 ; i <= n ; i ++) // 找出树的根节点的编号
if(fa[i] == i) {
root = i ;
break ;
}
string ans = dfs(root) ;
cout << ans << endl ;
return 0 ;
}
The result :
原文地址:https://www.cnblogs.com/lingchen1642/p/15227370.html
- 一次关于js事件出发机制反常的解决记录
- Java综合题目
- Ubuntu 16.04下为TITAN 1080 显卡安装驱动及Gpu版TensorFlow|深度学习
- 给新生成的节点(动态生成节点)绑定事件方法总结
- JavaWeb(四)JDBC操作Oracle
- JavaWeb(六)Listener监听器
- JSP引入 - UEditor 富文本编辑器
- JavaWeb(五)Filter过滤器
- JavaWeb(七)Cookie,EL表达式,标准标签库
- JavaWeb(八)JQuery
- 程序员如何提一个好问题
- JavaWeb(九)AJAX
- 国内外免费地图SDK都在这了,开发APP再也不怕找不到路了
- Hibernate框架HQL语句
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- R语言用于线性回归的稳健方差估计
- 用SAS进行泊松,零膨胀泊松和有限混合Poisson模型分析
- 东芝开发板驱动OLED模块显示LOGO图片
- sas文本挖掘案例:如何使用SAS计算Word Mover的距离
- R语言ggplot2 对Facebook用户数据可视化分析
- 如何实现一个圆弧倒计时进度条
- R语言Kaggle泰坦尼克号性别阶级模型数据分析案例
- 以图搜图系统概述
- GitHub Pages 配置 letsencrypt 开启HTTPS
- R语言中ARMA,ARIMA(Box-Jenkins),SARIMA和ARIMAX模型用于预测时间序列数据
- 以图搜图系统工程实践
- R语言线性判别分析(LDA),二次判别分析(QDA)和正则判别分析(RDA)
- 用R语言实现神经网络预测股票实例
- R语言社区主题检测算法应用案例
- C++ vector学习笔记