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 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的更多相关文章

  1. uva 11234 Expressions 表达式 建树+BFS层次遍历

    题目给出一个后缀表达式,让你求从下往上的层次遍历. 思路:结构体建树,然后用数组进行BFS进行层次遍历,最后把数组倒着输出就行了. uva过了,poj老是超时,郁闷. 代码: #include < ...

  2. UVa 11234 Expressions (二叉树重建&由叶往根的层次遍历)

    画图出来后结果很明显 xyPzwIM abcABdefgCDEF sample output wzyxIPM gfCecbDdAaEBF * + - x y z w F B E a A d D b c ...

  3. UVa 11234 The Largest Clique

    找最长的连接的点的数量.用tarjan缩点,思考可知每一个强连通分量里的点要么都选,要么都不选(走别的路),可以动规解决. #include<iostream> #include<c ...

  4. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  5. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第六章 1(Lists)

    127 - "Accordian" Patience 题目大意:一个人一张张发牌,如果这张牌与这张牌前面的一张或者前面的第三张(后面称之为一位置和三位置)的点数或花式相同,则将这张 ...

  6. UVA 327 -Evaluating Simple C Expressions(栈)

    Evaluating Simple C Expressions The task in this problem is to evaluate a sequence of simple C expre ...

  7. uva 327 - Evaluating Simple C Expressions

     Evaluating Simple C Expressions  The task in this problem is to evaluate a sequence of simple C exp ...

  8. uva 327 Evaluating Simple C Expressions 简易C表达式计算 stl模拟

    由于没有括号,只有+,-,++,--,优先级简单,所以处理起来很简单. 题目要求计算表达式的值以及涉及到的变量的值. 我这题使用stl的string进行实现,随便进行练手,用string的erase删 ...

  9. UVa 112 - Tree Summing(树的各路径求和,递归)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  10. UVA 442 二十 Matrix Chain Multiplication

    Matrix Chain Multiplication Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

随机推荐

  1. dumpbin

    在使用VC时,可以用DUMPBIN.EXE来得到某个DLL中所输出的符号的清单.如下面的 命令:dumpbin -exports Cmpnt1.dll如: C:/WINDOWS/system32> ...

  2. C++自删除

    #pragma once class AutoRelease { public: AutoRelease(void){ m_count = 0; } virtual ~AutoRelease(void ...

  3. Android实战技巧: ListView之ContextMenu无法弹出

    问题 Activity中使用了ListView作为布局.当每一列表项中含有默认能获取焦点的子View时有可能会对ListView的某些事件有影响: 1. OnItemClick 2. OnItemLo ...

  4. sql plus 抢救数据(測)

    有索引 --运行plsql脚本 DECLARE  TYPE RowIDTab ISTABLE OF ROWID INDEXBY BINARY_INTEGER;    CURSORc1 IS selec ...

  5. 一个用 C 语言写的迷你版 2048 游戏,仅仅有 500个字符

    Jay Chan 用 C 语言写的一个迷你版 2048 游戏,仅仅有 487 个字符. 来围观吧 M[16],X=16,W,k;main(){T(system("stty cbreak&qu ...

  6. 每天一个JavaScript实例-动态省份选择城市

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. 关于Oralce数据库优化的几点总结

    个人理解,数据库性能最关键的因素在于IO,因为操作内存是快速的,但是读写磁盘是速度很慢的,优化数据库最关键的问题在于减少磁盘的IO,就个人理解应该分为物理的和逻辑的优化, 物理的是指oracle产品本 ...

  8. [poj 2991]Crane[线段树表示向量之和,而非数量]

    题意: 起重机的机械臂, 由n段组成, 对某一些连接点进行旋转, 询问每次操作后的末端坐标. 思路: 由于旋转会影响到该点之后所有线段的角度, 因此容易想到用线段树记录角度, 成段更新. (但是不是每 ...

  9. hdu2639(背包求第k优解)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:给出一行价值,一行体积,让你在v体积的范围内找出第k大的值 分析:dp[i][j][k]表 ...

  10. Nginx使用ngx_zeromq模块返回502错误的解决方法

    /*********************************************************************  * Author  : Samson  * Date   ...