uva-11234 Expressions
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:
- push: a number is inserted at the top of the stack.
- 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:
- push: a number is inserted at the end of the queue.
- 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 Specification
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 Specification
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
题目意思:求一个字符序列,该序列使得当它使用队列且使用与栈相同的操作序列,表达示结果一样。
解题思路:当你建立表达示树的时候,不难发现一个规律,所求的字符序列是表达示树的层序遍历产生的序列的逆序。
所以我们先建树,然后层序遍历就行了。
#include <iostream>
#include<deque>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<sstream>
#include<cctype>
#include<queue>
using namespace std; struct node
{
char data;
node*left;
node*right;
node(int d):data(d),left(0),right(0){};
}; node* build(string s)
{
stack<node*>st;
for(unsigned i=0;i<s.size();i++)
{
if(isupper(s[i]))
{
node*right=st.top();st.pop();
node*left=st.top();st.pop();
node*root=new node(s[i]);
root->right=right;
root->left=left;
st.push(root);
}
else
{
st.push(new node(s[i]));
}
}
return st.top();
} string get_ans(node* root)
{
queue<node*>q;
q.push(root);
string ans;
while(!q.empty())
{
node*cur=q.front();q.pop();
ans+=cur->data;
if(cur->left)q.push(cur->left);
if(cur->right)q.push(cur->right);
}
reverse(ans.begin(),ans.end());
return ans;
} int main()
{
int n;
string s;
cin>>n;
while(n--)
{
string ans;
cin>>s;
node* root=build(s);
cout<<get_ans(root)<<endl;
}
return 0;
}
uva-11234 Expressions的更多相关文章
- uva 11234 Expressions 表达式 建树+BFS层次遍历
题目给出一个后缀表达式,让你求从下往上的层次遍历. 思路:结构体建树,然后用数组进行BFS进行层次遍历,最后把数组倒着输出就行了. uva过了,poj老是超时,郁闷. 代码: #include < ...
- UVa 11234 Expressions (二叉树重建&由叶往根的层次遍历)
画图出来后结果很明显 xyPzwIM abcABdefgCDEF sample output wzyxIPM gfCecbDdAaEBF * + - x y z w F B E a A d D b c ...
- UVa 11234 The Largest Clique
找最长的连接的点的数量.用tarjan缩点,思考可知每一个强连通分量里的点要么都选,要么都不选(走别的路),可以动规解决. #include<iostream> #include<c ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第六章 1(Lists)
127 - "Accordian" Patience 题目大意:一个人一张张发牌,如果这张牌与这张牌前面的一张或者前面的第三张(后面称之为一位置和三位置)的点数或花式相同,则将这张 ...
- UVA 327 -Evaluating Simple C Expressions(栈)
Evaluating Simple C Expressions The task in this problem is to evaluate a sequence of simple C expre ...
- uva 327 - Evaluating Simple C Expressions
Evaluating Simple C Expressions The task in this problem is to evaluate a sequence of simple C exp ...
- uva 327 Evaluating Simple C Expressions 简易C表达式计算 stl模拟
由于没有括号,只有+,-,++,--,优先级简单,所以处理起来很简单. 题目要求计算表达式的值以及涉及到的变量的值. 我这题使用stl的string进行实现,随便进行练手,用string的erase删 ...
- UVa 112 - Tree Summing(树的各路径求和,递归)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- UVA 442 二十 Matrix Chain Multiplication
Matrix Chain Multiplication Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
随机推荐
- c#调用语音功能
转自 http://www.cnblogs.com/Hans2Rose/p/WeatherSpeaker.html .Net里面自带了一个语音类库:System.Speech,调用系统的语音功能,就能 ...
- 检查java class的版本号
补丁总是会一遍又一遍的打,越打越多 有时候,就担心有人不小心把高版本的class打到低版本jre运行的环境中 简单写了点代码,检查文件夹中class的版本号 package org.wee.cv; i ...
- sort如何按指定的列排序·百家电脑学院
sort如何按指定的列排序·百家电脑学院 sort如何按指定的(9php.com)列排序 0000 27189 41925425065f ...
- onkeypress事件.onkeydown事件.onkeyup事件
onkeypress事件是在按键開始按的时候发生: onkeydown事件是在按键已经按下的时候发生: onkeyup事件是在按键松开(释放)的时候发生.
- (二十一)unity4.6学习Ugui中文文档-------交互-Supported Events & Raycasters
大家好,我是孙广东. 转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:mod=guide&view=m ...
- 基于CORS的geoserver同源访问策略
这个问题理顺整个2天.终于攻克.记录下来. 1.下载文件 首先下载cors压缩包,解压,得到的是org/mortbay/servlets/CrossOriginFilter.class文件,把此文件拷 ...
- Wix打包系列(五) 部署数据库
原文:Wix打包系列(五) 部署数据库 很多人在使用vs进行打包的时候,经常会为数据库部署的问题犯愁,即便是重写Installer类的方法,也不是很可靠方便,下面我们来看看在wix中如何部署数据库. ...
- Wix打包系列(三)自定义Action(Custom Action)
原文:Wix打包系列(三)自定义Action(Custom Action) 3.1 关于Action 我们已经知道如何生成具有标准安装界面的安装程序了,Windows Installer按照我们的界面 ...
- oracle undo 复杂度--oracle核心技术读书笔记四
一. 概述 undo 保存的是旧数据.比方,你改动了一条记录将A列abc改动为def,那么undo里面保存的就是abc.目的有两个:1. 假设你的事务没有提交,可是已经将A列改动,那么别人读取这条数据 ...
- EJBTimer 使用EJB提供的定时器
一.说明 EJB提供的定时器有两种,自动定时器和自定义定时器,自动定时器设置使用简单但是扩展较为麻烦,自定义定时器有较好的扩展性. 下面的例子中是把两中方式放到了一个测试类中. 二.示例 import ...