Unique Encryption Keys

#include <iostream>
#include <string.h>
#include <vector>
#include <cstdio>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <stack>
using namespace std;
typedef long long ll;
const ll  Max=1000000000;
vector<string>  op;
vector<ll >  num;
bool ok(ll x)
{
    if(x>Max||x<-Max)return false;
    return true;
}


ll solve(ll  t )
{
    if(!ok(t))return Max+1;
    stack<ll>  q;
    q.push(t);
    int j=0;
    for(int i=0; i<op.size(); i++)
    {
        if(op[i]=="NUM")
        {
            if(ok(num[j]))
                q.push(num[j]),j++;
            else
                return Max+1;
        }
        else if(op[i]=="POP")
        {
            if(!q.empty())
                q.pop();
            else
                return Max+1;
        }
        else if(op[i]=="INV")
        {
            if(!q.empty())
            {
                ll tmp=q.top();
                q.pop();
                q.push(-tmp);
            }
            else
                return Max+1;
        }
        else if(op[i]=="DUP")
        {
            if(!q.empty())
            {
                ll tmp=q.top();
                q.push(tmp);
            }
            else
                return Max+1;
        }
        else if(op[i]=="SWP")
        {
            ll tmp,tmp1;
            if(!q.empty())
            {
                tmp=q.top();
                q.pop();
            }
            else
                return Max+1;
            if(!q.empty())
            {
                tmp1=q.top();
                q.pop();
            }
            else
                return Max+1;
            q.push(tmp);
            q.push(tmp1);
        }
        else if(op[i]=="ADD")
        {
            ll tmp,tmp1;
            if(!q.empty())
            {
                tmp=q.top();
                q.pop();
            }
            else
                return Max+1;
            if(!q.empty())
            {
                tmp1=q.top();
                q.pop();
            }
            else
                return Max+1;
            if(ok(tmp+tmp1))
                q.push(tmp+tmp1);
            else
                return Max+1;
        }
        else if(op[i]=="SUB")
        {
            ll tmp,tmp1;
            if(!q.empty())
            {
                tmp=q.top();
                q.pop();
            }
            else
                return Max+1;
            if(!q.empty())
            {
                tmp1=q.top();
                q.pop();
            }
            else
                return Max+1;
            if(ok(tmp1-tmp))
                q.push(tmp1-tmp);
            else
                return Max+1;
        }
        else if(op[i]=="MUL")
        {
            ll  tmp,tmp1;
            if(!q.empty())
            {
                tmp=q.top();
                q.pop();
            }
            else
                return Max+1;
            if(!q.empty())
            {
                tmp1=q.top();
                q.pop();
            }
            else
                return Max+1;
            if(ok(tmp*tmp1))
                q.push(tmp*tmp1);
            else
                return Max+1;
        }
        else if(op[i]=="DIV")
        {
            ll tmp,tmp1;
            if(!q.empty())
            {
                tmp=q.top();
                q.pop();
            }
            else
                return Max+1;
            if(!q.empty())
            {
                tmp1=q.top();
                q.pop();
            }
            else
                return Max+1;
            if(tmp==0)return Max+1;
            if(ok(tmp1/tmp))
                q.push(tmp1/tmp);
            else
                return Max+1;
        }
        else if(op[i]=="MOD")
        {
            ll tmp,tmp1;
            if(!q.empty())
            {
                tmp=q.top();
                q.pop();
            }
            else
                return Max+1;
            if(!q.empty())
            {
                tmp1=q.top();
                q.pop();
            }
            else
                return Max+1;
            if(tmp==0)return Max+1;
            ll  c=tmp1/tmp;
            if(ok(tmp1-c*tmp))
                q.push(tmp1-c*tmp);
            else
                return Max+1;
        }
    }
    int sum=0;
    ll ans;
    while(!q.empty())
    {
        ans=q.top();
        q.pop();
        sum++;
    }
    if(sum==1&&ok(ans))return ans;
    return Max+1;
}


int main()
{
    op.clear();
    num.clear();
    ll t;
    string a;
    ll n;
    while(cin>>a)
    {
        if(a=="QUIT")break;
        if(a!="END")
        {
            op.push_back(a);
            if(a=="NUM")
            {
                scanf("%lld",&n);
                num.push_back(n);
            }
        }
        else
        {
            scanf("%lld",&n);
            for(ll i=0; i<n; i++)
            {
                scanf("%lld",&t);
                ll ans=solve(t);
                if(ans>Max)
                    puts("ERROR");
                else
                    printf("%lld\n",ans);


            }
            puts("");
            op.clear();
            num.clear();
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值