HDU-4192 Guess the Numbers 中缀转后缀

         题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4192

         题意:给以一个表达式,要你求表达式是否成立。

         典型的中缀表达式转后缀表达式,表达式树也可以搞搞。

   表达式由中缀转化为后缀。设一个stack存后缀数据,一个rout栈存运算符。
   方法:

(1)从右向左依次取得数据ch。
(2)如果ch是操作数,直接加进stack中。

(3)如果ch是运算符(含左右括号),则:
         a:如果ch = '(',放入堆栈rout中。
         b:如果ch = ')',依次输出堆栈rout中的运算符,直到遇到'('为止。
         c:如果ch不是')'或者'(',那么就和堆栈rout顶点位置的运算符top做优先级比较。
                  1:如果ch优先级比rtop高,那么将ch放入堆栈rout。
                  2:如果ch优先级低于或者等于rtop,那么输出top到stack中(直到!top或者满足 1),然后将ch放入堆栈rout。

       My code:

//STATUS:C++_AC_31MS_208KB
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)<(y)?(x):(y))
#define LL __int64
#define pii pair<int,int>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
const int MAX=110,INF=200000000,MOD=100000007;
const double esp=1e-6;

int getsta(char *s);
int getsum(int top);
int cal(int op,int a,int b);
int sta[MAX];
int pri[110],num[10],ch[10],val[210];
int n,m,len,top;
char s[MAX];

int main()
{
//    freopen("in.txt","r",stdin);
    pri['*']=2,pri['+']=1,pri['-']=1;
    int i,j,ok;
    while(~scanf("%d",&n))
    {
        for(i=0;i<n;i++)
            scanf("%d",&num[i]);
        scanf("%d",&m);
        if(!n && !m)break;
        scanf("%s",s);
        len=strlen(s);
        for(i=j=0;i<len;i++)
            if(s[i]>='a' && s[i]<='z'){
                ch[j++]=s[i];
            }

        ok=0;
        sort(num,num+n);
        getsta(s);
        do{
            for(i=0;i<n;i++)
                val[ch[i]]=num[i];
            if(getsum(top)==m){
                ok=1;
                printf("YES\n");
                break;
            }
        }while(next_permutation(num,num+n));
        if(!ok)
            printf("NO\n");
    }
    return 0;
}

int getsta(char *s)
{
    int i,j;
    int rout[MAX];
    int tops=-1,topr=-1;
    for(i=0;i<len;i++){
        if(s[i]>='a' && s[i]<='z'){
            sta[++tops]=s[i];
        }
        else{
            if(s[i]=='('){
                rout[++topr]=s[i];
            }
            else if(s[i]==')'){
                while(rout[topr]!='(' && topr>=0){
                    sta[++tops]=-rout[topr--];
                }
                topr--;
            }
            else{
                while(topr>=0 && rout[topr]!='(' && pri[s[i]]<=pri[rout[topr]]){
                    sta[++tops]=-rout[topr--];
                }
                rout[++topr]=s[i];
            }
        }
    }
    while(topr>=0){
        sta[++tops]=-rout[topr--];
    }
    top=tops;
    return 1;
}

int getsum(int top)
{
    int i,j,k;
    int stat[MAX],vis[MAX],x[2];
    for(i=0;i<=top;i++){
        stat[i]=sta[i];
        vis[i]=stat[i]>0?1:0;
    }
    for(i=0;i<=top;i++){
        if(stat[i]<0){
            for(j=i-1,k=0;j>=0 && k<2;j--){
                if(vis[j]){
                    vis[j]=0;
                    x[k++]=stat[j];
                }
            }
            vis[i]=1;
            stat[i]=cal(-stat[i],x[1],x[0]);
        }
        else stat[i]=val[stat[i]];
    }
    return stat[top];
}

int cal(int op,int a,int b){
    if(op=='*')
        return a*b;
    if(op=='+')
        return a+b;
    if(op=='-')
        return a-b;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值