Expressions uva

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:

  1. push: a number is inserted at the top of thestack.
  2. 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:

  1. push: a number is inserted at the end of thequeue.
  2. 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值