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:
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
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
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 <bits/stdc++.h>

using namespace std;

typedef long long ll;
string s;
int n;

void eli_bra(string &x)//去括号
{
    x=x.substr(1,x.size()-2);
}


//从org中分离出操作符和表达式,如
//(aB(qWe))G(dEf) 分离后得到 (aB(qWe)) G 和 (dEf)
void split(string &org,string &s1,string &o,string &s2)
{
    s1.clear();
    o.clear();
    s2.clear();
    if(org[0]!='(')
    {
        s1+=org[0];
        o+=org[1];
        s2=org.substr(2,org.size()-1);
        return;
    }
    int cnt=1;
    s1+=org[0];
    for(int i=1;i<org.size();i++)
    {
        if(org[i]=='(')
            cnt++;
        if(org[i]==')')
            cnt--;
        s1+=org[i];
        if(cnt==0)
        {
            o+=org[i+1];
            s2+=org.substr(i+2,org.size()-(i+2));
            return;
        }
    }
    return ;
}

int main()
{

    ios::sync_with_stdio(false);
    cin>>n;
    while(n--)
    {
        cin>>s;
        string post,tmp,s1,s2;
        deque<string> sc;
        string ans="";
        for(int i=0;i<s.size();i++)
        {
            tmp.clear();
            tmp+=s[i];
            if(i==s.size()-1)//最后一个操作符
            {
                ans=tmp;
                break;
            }
            if(islower(s[i]))
            {
                sc.push_back(tmp);
            }
            else
            {
                s1=sc.back();
                sc.pop_back();
                s2=sc.back();
                sc.pop_back();
                sc.push_back("("+s2+tmp+s1+")");
            }
        }
        tmp.clear();
        s1.clear();
        s2.clear();
        string f,o;
        while(!sc.empty())
        {
            f=sc.front();
            sc.pop_front();
            if(f.size()==1)
            {
                ans=f+ans;
                continue;
            }
            eli_bra(f);
            split(f,s1,o,s2);
            ans=o+ans;
            sc.push_back(s1);
            sc.push_back(s2);
        }
        cout<<ans<<endl;

    }

    return 0;
}

解答:

首先将后缀表达式转换成中缀表达式,也就是咱们日常计算的那种式子(带括号的那种),再将得到的中缀表达式逆着模拟回去。如果不知道怎么模拟,可以算算样例,在纸上用队列算出样例答案的过程,然后再反向搞回去即可。

例如样例1
得到的中缀表达式是((xPy)M(zIw))
根据例1的结果,使用队列的推导过程如下

使用队列计算wzyxIPM

"xyzw入队"
|x|
|y|
|z|
|w|
"遇到操作符I"
|(zIw)|
|  x  |
|  y  |
"遇到操作符P"
|(xPy)|
|(zIw)|
"遇到操作符M"
|((xPy)M(zIw))|

反着的过程模拟一下就好
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值