本代码在ide运行与评测结果不一致
查看原帖
本代码在ide运行与评测结果不一致
775991
jianamisabina楼主2023/8/22 23:06

rt 在跑 #1 时,ide 输出 ABDEF,提交会 RE。

本代码可以在校内 OJ 上通过。

#include <iostream>
#include <map>
#include <stack>
#define ll long long
using namespace std;
const int Mod = 11451,sp = 10086;
//sp 是特殊值
stack <int> s1;
stack <char> s2;
map <char,int> yxj;

int calc(int x,int y,char op){
    if(op == '+') return x + y;
    else if(op == '-') return (x - y + Mod) % Mod;
    else if(op == '*') return x * y;
    else{
        int sum = 1;
        for(int i = 1;i <= y;i ++) sum *= x,sum %= Mod;
        return sum;
    }
}

int bds(string str){
    yxj['+'] = 1;
    yxj['-'] = 1;
    yxj['*'] = 2;
    yxj['/'] = 2;
    yxj['^'] = 3;
    yxj['('] = 0;

    int len = str.size();
    //str += ' ';
    for(int i = 0;i < len;i ++){
            //cerr << i;
            if(str[i] == ' ') continue;
        if(str[i] == 'a') s1.push(sp);
        else if(str[i] >= '0' && str[i] <= '9'){
            int sum = 0;
            while(str[i] >= '0' && str[i] <= '9'){
                //if(str[i] >= '0' && str[i] <= '9') break;
                sum = sum * 10 + str[i] - '0';
                i ++;
            }i --;
            s1.push(sum);
        }
        else if(str[i] == '(') s2.push(str[i]);
        else if(str[i] != ')'){
            while(!s2.empty() && yxj[s2.top()] >= yxj[str[i]]){
                int b = s1.top();s1.pop();
                int a = s1.top();s1.pop();
                int op = s2.top();s2.pop();
                s1.push(calc(a,b,op) % Mod);
            }s2.push(str[i]);
        }else{
            while(!s2.empty() && s2.top() != '('){
                int b = s1.top();s1.pop();
                int a = s1.top();s1.pop();
                int op = s2.top();s2.pop();
                s1.push(calc(a,b,op) % Mod);
            }if(s2.size()) s2.pop();
        }
    }
    while(!s2.empty()){
        if(s2.top() == '('){
            s2.pop();continue;
        }
        int b = s1.top();s1.pop();
        int a = s1.top();s1.pop();
        int op = s2.top();s2.pop();
        s1.push(calc(a,b,op) % Mod);
    }
    return s1.top();
}

int main(){
    string s;
    getline(cin,s);
    int goal = bds(s);
    int n;
    cin >> n;
    string test;
    getline(cin,test);
    for(int i = 1;i <= n;i ++){
         getline(cin,test);
        int ans = bds(test);
        if(goal == ans){
            cout << (char)('A' + i - 1);
        }
    }
    return 0;
}

2023/8/22 23:06
加载中...