WA 80pts 求调
查看原帖
WA 80pts 求调
1101572
zhaisx楼主2024/8/7 08:25
#include <bits/stdc++.h>
using namespace std;

const int mod = 10007;

class Expression {
  private:
    stack<int> one, zero;
    stack<char> sym;
    string s;

    bool ge(char a, char b) {
        if (b == ' ' || b == '(')
            return true;
        if (a == '*' && b == '+')
            return true;
        return false;
    }

    void compute(char sym) {
        int o1 = one.top();
        one.pop();
        int o2 = one.top();
        one.pop();
        int z1 = zero.top();
        zero.pop();
        int z2 = zero.top();
        zero.pop();
        if (sym == '+') {
            zero.push(z1 * z2 % mod);
            one.push((z1 * o2 % mod + o1 * z2 % mod + o1 * o2 % mod) % mod);
        }
        if (sym == '*') {
            zero.push((z1 * z2 % mod + z1 * o2 % mod + o1 * z2 % mod) % mod);
            one.push(o1 * o2 % mod);
        }
    }

    void get_bracket_value() {
        char t;
        int a, b;
        while ((t = sym.top()) != '(') {
            if (t == ')') {
                get_bracket_value();
                continue;
            }
            sym.pop();
            compute(t);
        }
        sym.pop();
    }

  public:
    Expression(string str) : s(str) {}

    int run() {
        sym.push(' ');
        one.push(1);
        zero.push(1);
        for (char c : s) {
            if (c == '(') {
                sym.push(c);
                one.push(1);
                zero.push(1);
                continue;
            }
            if (c == ')') {
                get_bracket_value();
                continue;
            }
            if (!ge(c, sym.top())) {
                compute(sym.top());
                sym.pop();
            }
            sym.push(c);
            one.push(1);
            zero.push(1);
        }
        while (sym.top() != ' ') {
            compute(sym.top());
            sym.pop();
        }
        return zero.top();
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
#endif
    int n;
    cin >> n;
    string s;
    cin >> s;
    Expression expr(s);
    cout << expr.run() << '\n';
    return 0;
}
2024/8/7 08:25
加载中...