Expressions

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 253    Accepted Submission(s): 121

Problem Description
Arithmetic
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?

 
Input
The
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.

 
Output
For
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.

 
Sample Input
2
xyPzwIM
abcABdefgCDEF
 
Sample Output
wzyxIPM
gfCecbDdAaEBF
 
Source
 
代码:
 //#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(二叉树构造的后缀表达式)的更多相关文章

  1. Java实现后缀表达式建立表达式树

    概述 表达式树的特点:叶节点是操作数,其他节点为操作符.由于一般的操作符都是二元的,所以表达式树一般都是二叉树. 根据后缀表达式"ab+cde+**"建立一颗树 文字描述: 如同后 ...

  2. 数据结构(3) 第三天 栈的应用:就近匹配/中缀表达式转后缀表达式 、树/二叉树的概念、二叉树的递归与非递归遍历(DLR LDR LRD)、递归求叶子节点数目/二叉树高度/二叉树拷贝和释放

    01 上节课回顾 受限的线性表 栈和队列的链式存储其实就是链表 但是不能任意操作 所以叫受限的线性表 02 栈的应用_就近匹配 案例1就近匹配: #include <stdio.h> in ...

  3. scala写算法-从后缀表达式构造

    一个例子,比如ab+cde+**,这是一个后缀表达式,那么如何转换为一棵表达式树呢? 先上代码,再解释: object Main extends App{ import Tree.node def i ...

  4. Java堆栈的应用2----------中缀表达式转为后缀表达式的计算Java实现

    1.堆栈-Stack 堆栈(也简称作栈)是一种特殊的线性表,堆栈的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置进行插入和删除操作,而堆栈只允许在固定一端进行插入和删除 ...

  5. javascript使用栈结构将中缀表达式转换为后缀表达式并计算值

    1.概念 你可能听说过表达式,a+b,a+b*c这些,但是前缀表达式,前缀记法,中缀表达式,波兰式,后缀表达式,后缀记法,逆波兰式这些都是也是表达式. a+b,a+b*c这些看上去比较正常的是中缀表达 ...

  6. hdu-1237 简单计算器---中缀表达式转后缀表达式

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1237 题目大意: 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. 思路 ...

  7. 前缀、中缀、后缀表达式以及简单计算器的C++实现

    前缀表达式(波兰表达式).中缀表达式.后缀表达式(逆波兰表达式) 介绍 三种表达式都是四则运算的表达方式,用以四则运算表达式求值,即数学表达式的求解. 前缀表达式 前缀表达式是一种没有括号的算术表达式 ...

  8. c++实验4 栈及栈的应用+回文+中、后缀表达式

    栈及栈的应用+回文+中.后缀表达式 1.栈顺序存储结构的基本操作算法实现 (1)栈顺序存储结构的类定义: class SeqStack { private: int maxsize; DataType ...

  9. Atitti. 语法树AST、后缀表达式、DAG、三地址代码

    Atitti. 语法树AST.后缀表达式.DAG.三地址代码 抽象语法树的观点认为任何复杂的语句嵌套情况都可以借助于树的形式加以描述.确实,不得不承认应用抽象语法树可以使语句翻译变得相对容易,它很好地 ...

随机推荐

  1. 学习html5第一天

    HTMl5作为web标准的一种,在2004年诞生,web超文本应用技术工作组WHATWG将它发展起来,W3C由开始的不赞同到与WHATWG共同合作,并在2015年开始推广.并随着浏览器的不断支持和兼容 ...

  2. UVA 11404 五 Palindromic Subsequence

     Palindromic Subsequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu ...

  3. [SAP ABAP开发技术总结]报表事件

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. Android——android必看 各个控件属性(网上看到的文字,觉得挺好的,珍藏了)

    属性 值 说明 Android:orientation horizontal/vertical 设置布局水平还是垂直,默认是垂直 android:checked true/false 标记默认选中,如 ...

  5. WrapPanel流布局的一个简单例子

    <Window x:Class="WrapPanel.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2 ...

  6. Chrome浏览器的密码隐患

    谷歌浏览器的密码填充使得登陆账号很方便 但在你了解了Chrome的密码特性机制后,你该做点什么了 1.如何查看已保存的密码 Chrome 密码管理器的进入方式:右侧扳手图标→设置→显示高级设置→密码和 ...

  7. Android手机分辨率基础知识(DPI,DIP计算)三

    获得屏幕分辨率和密度,尺寸的代码片段 DisplayMetrics displayMetrics = new DisplayMetrics();getWindowManager().getDefaul ...

  8. hdu 1588(Fibonacci矩阵求和)

    题目的大意就是求等差数列对应的Fibonacci数值的和,容易知道Fibonacci对应的矩阵为[1,1,1,0],因为题目中f[0]=0,f[1]=1,所以推出最后结果f[n]=(A^n-1).a, ...

  9. easyui 进度条

    进度条创建 $.messager.progress({ title:'请稍后', msg:'正在努力...' }); 进度条关闭 $.messager.progress('close'); 弹窗对话框 ...

  10. Android 热补丁动态修复框架小结

    一.概述 最新github上开源了很多热补丁动态修复框架,大致有: https://github.com/dodola/HotFix https://github.com/jasonross/Nuwa ...