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.三地址代码 抽象语法树的观点认为任何复杂的语句嵌套情况都可以借助于树的形式加以描述.确实,不得不承认应用抽象语法树可以使语句翻译变得相对容易,它很好地 ...
随机推荐
- CodeForces 567C Geometric Progression
Geometric Progression Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- Keepalive
https://en.wikipedia.org/wiki/Keepalive Description A keepalive signal is often sent at predefined i ...
- 【原创】VB6.0应用程序安装包的生成(Setup Factory 9.0制作安装包的方法)
VB6.0应用程序安装包的生成,利用其自带的打包工具生成的安装程序很简陋,一点不美观:如果想让自己的应用程序安装的时候显得高大上一点,本教程提供使用Setup Factory 9.0制作安装包的方法. ...
- Codeforces Round #243 (Div. 2) B(思维模拟题)
http://codeforces.com/contest/426/problem/B B. Sereja and Mirroring time limit per test 1 second mem ...
- 杭电1466------简单的dp
题目: http://acm.hdu.edu.cn/showproblem.php?pid=1466 #include<iostream> #include<cstdio> # ...
- CSS的基本操作
<html> <!-- . 给整个页面填一个一个背景 . 给em添加一个样式样倾斜效果消失 . 改变第一层UL的样式为蓝色,16px . 改变第二层的UL的样式为红色 14px . ...
- SQL server数据类型、增删改查
数据类型: 整数型:bigint.int.smallint.mediumint.tinyint 小数类型:decimal.numeric 浮点型:real.float.double 位型:bit 字符 ...
- 一切都是对象 Thinking in Java 第二章
2.1 用引用操作对象 1.对象和引用联系起来,就可以通过引用来操作对象: 2.引用亦可单独存在,即没有和任何对象联系起来:对没有和对象联系起来的引用操作,会报错: 2.2 必须由你创建所有对象 1. ...
- (三)ubuntu学习前传—uboot常见环境变量
1.环境变量如何参与程序运行(1)环境变量有2份,一份在Flash中,另一份在DDR中.uboot开机时一次性从Flash中读取全部环境变量到DDR中作为环境变量的初始化值,然后使用过程中都是用DDR ...
- surfaceview介绍
[1]surfaceview 控件是一个重量级控件 [2]内部维护了2个线程 A 获取数据 负责显示 B 负责显示 获取数据 [3]他可以直接在子线程更新ui ...