11234 - Expressions
来源:互联网 发布:焦娇淘宝主播 编辑:程序博客网 时间:2024/06/06 02:04
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 10000characters.
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
2xyPzwIMabcABdefgCDEF
Sample Output
wzyxIPMgfCecbDdAaEBF
#include<iostream>#include<string>#include<stack>#include<queue>using namespace std;struct node{char value;node* left;node* right;};void BFtraverse(node *root,string &output){queue<node*> q;q.push(root);int i=0;while(!q.empty()){node *treenode=q.front();output[i]=treenode->value;q.pop();if(treenode->left!=NULL)q.push(treenode->left);if(treenode->right!=NULL)q.push(treenode->right);i++;}}int main(){int n;string postfix;cin>>n;for(int i=0;i<n;i++){cin>>postfix;stack<node*> expr;int count=0;while(count<postfix.size()){if(postfix[count]>='a' && postfix[count]<='z'){node *treenode=new node;treenode->left=treenode->right=NULL;treenode->value=postfix[count];expr.push(treenode);}else{node *treenode=new node;treenode->value=postfix[count];treenode->right=expr.top();expr.pop();treenode->left=expr.top();expr.pop();expr.push(treenode);}count++;}node *root=expr.top();BFtraverse(root,postfix);for(int j=postfix.size()-1;j>=0;j--)cout<<postfix[j];cout<<endl;}return 0;}
- 11234 - Expressions*****
- 11234 - Expressions
- 11234 - Expressions
- 11234 - Expressions
- UVa 11234 Expressions
- uva 11234 - Expressions
- UVa 11234 Expressions
- uva 11234 - Expressions
- UVa 11234 - Expressions
- UVA 11234 - Expressions
- UVa 11234 - Expressions
- UVA 11234 - Expressions
- uva 11234 - Expressions
- UVA 11234 - Expressions
- uva-11234 Expressions
- UVA 11234 Expressions
- UVa 11234 - Expressions
- UVa 11234 Expressions
- PHP中常用的类型转换
- 立此存照(10)[C++]climits头文件以及5中基本类型的长度与极值
- 11111 - Generalized Matrioshkas
- 从LongAdder看更高效的无锁实现
- 设计模式: 观察者模式
- 11234 - Expressions
- HTTP协议学习笔记(一)
- 矩阵乘法
- CToolTipCtrl的小技巧
- OpenCV 2.4.8 +VS2010的开发环境配置
- ural1051(数学题)
- leetcode-Maximum Product Subarray
- 第五章 5.4.1节练习 & 5.4.2节练习
- struts2.0的工作原理