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.三地址代码 抽象语法树的观点认为任何复杂的语句嵌套情况都可以借助于树的形式加以描述.确实,不得不承认应用抽象语法树可以使语句翻译变得相对容易,它很好地 ...
随机推荐
- ProgressBar 的使用
ProgressBar 的使用方法 转载:http://blog.csdn.net/mad1989/article/details/38042875
- 【转载】Linux系统启动流程
原文:Linux系统启动流程 POST(Power On Self Test/上电自检)-->BootLoader(MBR)-->Kernel(硬件探测.加载驱动.挂载根文件系统./sbi ...
- 机器学习——SVM详解(标准形式,对偶形式,Kernel及Soft Margin)
(写在前面:机器学习入行快2年了,多多少少用过一些算法,但由于敲公式太过浪费时间,所以一直搁置了开一个机器学习系列的博客.但是现在毕竟是电子化的时代,也不可能每时每刻都带着自己的记事本.如果可以掏出手 ...
- Upgrade R (升级R语言)
R R version 3.1.1 (2014-07-10) -- "Sock it to Me" yum list installed | grep R R-core.x86_6 ...
- Codeforces Round #376 (Div. 2) C. Socks bfs
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- BeanUtils框架的简单运用
Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单.易用的API操作Bean的属性——BeanUtils Beanutils工具包的常用类: •BeanUt ...
- 原生js如何获取当前所加载网页的文件路径和名称
结合使用string对象中的substr()和lastIndexOf()方法. 当前页面路径:file:///C:/Users/Administrator/Desktop/test.html < ...
- iOS - UIDevice
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIDevice : NSObject @available(iOS 2.0, *) public class UI ...
- html5 的draggable属性使用<转载收藏>
在HTML5中,已经支持在浏览器与其他应用程序之间的数据互相拖动,同时也大大简化了有关于拖放方面的代码. 实现拖放的步骤 在HTML5中要想实现拖放操作,至少要经过两个步骤: 将想要拖放的对象元素的d ...
- SG 复习全部 (全部SG 总览)
1. SQL 基础 进入查缺补漏阶段 2. PL/SQL 进入 practice 阶段 3. Fundamental 1 这部分还是比较重要 architecture 数据库启动与关闭步骤 insta ...