hdu 1805Expressions(二叉树构造的后缀表达式)
Expressions
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 253 Accepted Submission(s): 121
expressions are usually written with the operators in between the two
operands (which is called infix notation). For example, (x+y)*(z-w) is
an arithmetic expression in infix notation. However, it is easier to
write a program to evaluate an expression if the expression is written
in postfix notation (also known as reverse polish notation). In postfix
notation, an operator is written behind its two operands, which may be
expressions themselves. For example, x y + z w - * is a postfix notation
of the arithmetic expression given above. Note that in this case
parentheses are not required.
To evaluate an expression written
in postfix notation, an algorithm operating on a stack can be used. A
stack is a data structure which supports two operations:
1. push: a number is inserted at the top of the stack.
2. pop: the number from the top of the stack is taken out.
During
the evaluation, we process the expression from left to right. If we
encounter a number, we push it onto the stack. If we encounter an
operator, we pop the first two numbers from the stack, apply the
operator on them, and push the result back onto the stack. More
specifically, the following pseudocode shows how to handle the case when
we encounter an operator O:
a := pop();
b := pop();
push(b O a);
The result of the expression will be left as the only number on the stack.
Now
imagine that we use a queue instead of the stack. A queue also has a
push and pop operation, but their meaning is different:
1. push: a number is inserted at the end of the queue.
2. pop: the number from the front of the queue is taken out of the queue.
Can
you rewrite the given expression such that the result of the algorithm
using the queue is the same as the result of the original expression
evaluated using the algorithm with the stack?
first line of the input contains a number T (T ≤ 200). The following T
lines each contain one expression in postfix notation. Arithmetic
operators are represented by uppercase letters, numbers are represented
by lowercase letters. You may assume that the length of each expression
is less than 10000 characters.
each given expression, print the expression with the equivalent result
when using the algorithm with the queue instead of the stack. To make
the solution unique, you are not allowed to assume that the operators
are associative or commutative.
xyPzwIM
abcABdefgCDEF
gfCecbDdAaEBF
//#define LOCAL
#include<cstdio>
#include<stack>
#include<cstring>
#include<cctype>
#include<queue>
using namespace std;
const int maxn=;
char ss[maxn];
char ans[maxn];
stack<int>op;
struct node
{
char da;
int lef; //该节点的左孩子
int rig; //该节点的有孩子
}str[maxn];
void bfs(int pos)
{
queue<int>sac;
sac.push(pos);
int tt=;
while(!sac.empty())
{
int i=sac.front();
ans[tt++]=str[i].da;
sac.pop();
if(str[i].lef!=str[i].rig)
{
sac.push(str[i].lef);
sac.push(str[i].rig);
}
}
while(pos>=)
printf("%c",ans[pos--]);
}
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int cas,i;
scanf("%d",&cas);
while(cas--)
{
scanf("%s",ss);
for(i=;ss[i];i++)
{
if(islower(ss[i]))
{
op.push(i);
str[i].da=ss[i];
str[i].lef=str[i].rig=-; //没有孩子
}
else
{
str[i].da=ss[i];
str[i].rig=op.top();
op.pop();
str[i].lef=op.top();
op.pop();
op.push(i);
}
}
bfs(i-);
puts("");
}
return ;
}
hdu 1805Expressions(二叉树构造的后缀表达式)的更多相关文章
- Java实现后缀表达式建立表达式树
概述 表达式树的特点:叶节点是操作数,其他节点为操作符.由于一般的操作符都是二元的,所以表达式树一般都是二叉树. 根据后缀表达式"ab+cde+**"建立一颗树 文字描述: 如同后 ...
- 数据结构(3) 第三天 栈的应用:就近匹配/中缀表达式转后缀表达式 、树/二叉树的概念、二叉树的递归与非递归遍历(DLR LDR LRD)、递归求叶子节点数目/二叉树高度/二叉树拷贝和释放
01 上节课回顾 受限的线性表 栈和队列的链式存储其实就是链表 但是不能任意操作 所以叫受限的线性表 02 栈的应用_就近匹配 案例1就近匹配: #include <stdio.h> in ...
- scala写算法-从后缀表达式构造
一个例子,比如ab+cde+**,这是一个后缀表达式,那么如何转换为一棵表达式树呢? 先上代码,再解释: object Main extends App{ import Tree.node def i ...
- Java堆栈的应用2----------中缀表达式转为后缀表达式的计算Java实现
1.堆栈-Stack 堆栈(也简称作栈)是一种特殊的线性表,堆栈的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置进行插入和删除操作,而堆栈只允许在固定一端进行插入和删除 ...
- javascript使用栈结构将中缀表达式转换为后缀表达式并计算值
1.概念 你可能听说过表达式,a+b,a+b*c这些,但是前缀表达式,前缀记法,中缀表达式,波兰式,后缀表达式,后缀记法,逆波兰式这些都是也是表达式. a+b,a+b*c这些看上去比较正常的是中缀表达 ...
- hdu-1237 简单计算器---中缀表达式转后缀表达式
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1237 题目大意: 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. 思路 ...
- 前缀、中缀、后缀表达式以及简单计算器的C++实现
前缀表达式(波兰表达式).中缀表达式.后缀表达式(逆波兰表达式) 介绍 三种表达式都是四则运算的表达方式,用以四则运算表达式求值,即数学表达式的求解. 前缀表达式 前缀表达式是一种没有括号的算术表达式 ...
- c++实验4 栈及栈的应用+回文+中、后缀表达式
栈及栈的应用+回文+中.后缀表达式 1.栈顺序存储结构的基本操作算法实现 (1)栈顺序存储结构的类定义: class SeqStack { private: int maxsize; DataType ...
- Atitti. 语法树AST、后缀表达式、DAG、三地址代码
Atitti. 语法树AST.后缀表达式.DAG.三地址代码 抽象语法树的观点认为任何复杂的语句嵌套情况都可以借助于树的形式加以描述.确实,不得不承认应用抽象语法树可以使语句翻译变得相对容易,它很好地 ...
随机推荐
- Java-Eclipse插件开发学习笔记
Eclipse插件 学习笔记 作者 Rick- Bao 开始日期 2014年8月26日 结束日期 2014年8月27日 一 . CVS(current version system) 版本控制 ...
- Mysql 导入 MSSQL
.安装mysql数据库的ODBC驱动,mysql-connector-odbc--win32.msi .打开控制面板\管理工具\数据源ODBC,在用户DSN中添加一个MySQL ODBC .51数据源 ...
- 用ubuntu下载电影:磁力链接,torrent,迅雷链接
用ubuntu下载电影:磁力链接,torrent,迅雷链接 操作系统:Ubuntu 14.04 64位 需要软件:Ktorent, Amule 安装软件: sudo apt-get install k ...
- L0/L1/L2范数的联系与区别
L0/L1/L2范数的联系与区别 标签(空格分隔): 机器学习 最近快被各大公司的笔试题淹没了,其中有一道题是从贝叶斯先验,优化等各个方面比较L0.L1.L2范数的联系与区别. L0范数 L0范数表示 ...
- HTTP消息中header头部信息的讲解
HTTP Request的Header信息 1.HTTP请求方式 如下表: GET 向Web服务器请求一个文件 POST 向Web服务器发送数据让Web服务器进行处理 PUT 向Web服务器发送数据并 ...
- 关于oracle sql developer乱码的问题
写了一个sql查询,我擦居然乱码了 然后双击下这个框,居然又是中文: 有谁能够告诉我这是什么鬼
- 02~ 一步一步教你使用 SVN之SVN 的介绍
SVN的介绍 一.学习SVN的主要内容的介绍 1.基本操作:学习SVN有哪些基本操作 2.服务器端的安装配置:SVN中必不可少的2个环境之一,会在下面的课程中介绍服务器如何去安装和配置 3.客户端软件 ...
- Xcode:Foundation框架找不到,或者是自动提示出现问题
问题描述:Foundation框架找不到,或者是自动提示出现问题 之前的操作:手贱,不少心把编译器里面的源码改了 处理办法:清理缓存 缓存位置:点击桌面后,选择系统菜单栏:前往—电脑—硬盘—用户—ap ...
- HTML笔记(二) 在HTML中使用CSS
外部CSS: <head> <link rel="stylesheet" type="text/css" href="mystyle ...
- js中两个对象的比较
代码取自于underscore.js 1.8.3的isEqual函数. 做了一些小小的修改,主要是Function的比较修改. 自己也加了一些代码解读. <!DOCTYPE html> & ...