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 ...
随机推荐
- 利用WinDbg找出程序崩溃的代码行号
之前碰到论坛里有几个好友,说程序不时的崩溃,什么xxoo不能read的! 如果光要是这个内存地址,估计你会疯掉~~ 所以分享一下基本的调试技巧,需要准备的工具有WinDbg + VC6.0, 下面是自 ...
- 在iPhoneApp中加载PDF
原文: http://ios.biomsoft.com/2012/02/17/load-a-pdf-file-in-the-iphone-app-smoothly/ 本节将学习如何从服务器加载 pdf ...
- Codechef Not a Triangle题解
找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,可是时间效率就是O(n*n*n) ...
- Linux查看硬盘使用时间等信息
查看硬盘信息的很多命令,都需要root权限,如果普通用户无法看到信息,请切换至root: 1.查看硬盘使用时间等信息 硬盘使用时间很重要,硬盘理论寿命是3万小时以上 $ sudo smartctl - ...
- mmap。
linux mmap 内存映射 mmap() vs read()/write()/lseek() 通过strace统计系统调用的时候,常常能够看到mmap()与mmap2().系统调用mmap()能够 ...
- VSTO之旅系列(一):VSTO入门
原文:VSTO之旅系列(一):VSTO入门 引言: 因为工作的原因,这段时间一直在看VSTO的相关的内容的,因此希望通过这个系列来记录下我学习的过程和大家分享Office开发的相关知识,希望以后有朋友 ...
- [置顶] ANDROID 返回,菜单和HOME键的监听
------网上找了很多资料,项目中使用,最后将经验总结如下: 1,返回和菜单键是可以直接重写onKeyDown(int keyCode, KeyEvent event) 方法监听: @Overrid ...
- Deploy Oracle 10.2.0.5 DataGuard on Red Hat Enterprise Linux 6.4
系统:Red Hat Enterprise Linux 6.4 数据库:Oracle 10.2.0.5.0 Patch Set 4 主机:10dg1 192.168.1.91 10dg2192.168 ...
- Mutex(测量)
游标共享怎样使用Mutex kks 使用mutex以便保护对于下述基于parent cursor父游标和子游标child cursor的一系列操作 对于父游标parent cursor的操作: 基于发 ...
- TCP、UDP和HTTP
先来一个讲TCP.UDP和HTTP关系的 1.TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层. 在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议. 在传输层中 ...