Expressions
Arithmetic expressions are usually written with the operators inbetween thetwo 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 ifthe expression is written in postfix notation (also known as reversepolishnotation). In postfix notation, an operator is written behind its twooperands, which may be expressions themselves. For example, x y + zw- * is a postfix notation of the arithmetic expressiongiven above. Note that in this case parentheses are not required.
To evaluate an expression written in postfix notation, an algorithmoperating on a stack can be used. A stack is a data structure whichsupports two operations:
- push: a number is inserted at the top of thestack.
- pop: the number from the top of the stack istaken 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 anoperator, we pop the first two numbers from the stack, apply theoperatoron them, and pushthe result back onto the stack. More specifically, the followingpseudocodeshows 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 thestack.
Now imagine that we use a queue instead of the stack. A queue alsohas apush and pop operation, but their meaning is different:
- push: a number is inserted at the end of thequeue.
- pop: the number from the front of the queueis taken out of the queue.
Can you rewrite the given expression such that the result of thealgorithmusing the queue is the same as the result of the original expressionevaluatedusing the algorithm with the stack?
Input Specification
The first line of the input contains a number T (T≤ 200). The following Tlines eachcontain one expression in postfix notation. Arithmetic operators arerepresented by uppercase letters, numbers are represented by lowercaseletters.You may assume that the length of each expression is less than 10000characters.
Output Specification
For each given expression, print the expression with the equivalentresultwhen using the algorithm with the queue instead of the stack. To makethesolution unique, you are not allowed to assume that the operators areassociative or commutative.
Sample Input
2 xyPzwIM abcABdefgCDEF
Sample Output
wzyxIPM gfCecbDdAaEBF
解决方案:想到的建立二叉树,以’操作‘来做节点。可是建树还不太熟练,哎!要多多练习了。
#include<iostream>
#include<cstdio>
#include<queue>
#include<list>
#include<cstring>
#include<vector>
const int nmax=11000;
using namespace std;
char re[nmax];
int l[nmax],r[nmax],S[nmax],Q[nmax];
int main()
{
int n;
char exp[11000];
scanf("%d",&n);
while(n--)
{
int top=-1,tail=0,head=0;
scanf("%s",exp);
int len=strlen(exp);
for(int i=0;i<len;i++)
{
if(exp[i]>='a'&&exp[i]<='z'){
S[++top]=i;
l[i]=-1;r[i]=-1;//数节点没有孩子
}//若是数,则先入栈,等待操作出现
else {
r[i]=S[top];
l[i]=S[top-1];
top-=2;
S[++top]=i;//同时更新该节点在栈中的位置,方便下一个节点寻找孩子
}//若是操作,则做父节点,左孩子,右孩子分别指向top,和top-1;
}
re[len--]='\0';
Q[tail++]=S[top];
while(head!=tail)
{
int node=Q[head++];
re[len--]=exp[node];
if(l[node]!=-1) Q[tail++]=l[node];
if(r[node]!=-1) Q[tail++]=r[node];
}
cout<<re<<endl;
}
return 0;
}