洛谷 - T642387

· · 个人记录

题意

给定一个只有运算符 +、-、* 和括号的表达式。

求表达式的值对 10^9 + 7 取模后的结果。

思路

我们可以使用栈来解决这个问题。

有括号怎么办?

代码

#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
ll read(){ ll x = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-'){ f = -1; } ch = getchar(); } while(ch >= '0' && ch <= '9'){ x = x * 10 + ch - '0', ch = getchar(); } return x * f; }
void write(ll x) { if(x < 0){ putchar('-'), x = -x; } if(x > 9){ write(x / 10); } putchar(x % 10 + '0'); }
string reads(){ string s = ""; char ch = getchar(); while(ch != ' ' && ch != '\n'){ s += ch; ch = getchar(); } return s; }
void writes(string s){ for(int i = 0; i < s.size(); i ++){ putchar(s[i]); } }
ld readd(){ ld x = 0.0, f = 1.0; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-'){ f = -1.0; } ch = getchar(); } while(ch >= '0' && ch <= '9'){ x = x * 10 + ch - '0', ch = getchar(); } if(ch != '.'){ return x * f; } ld w = 10.0; ch = getchar(); while(ch >= '0' && ch <= '9'){ x = x + (ch - '0') / w; w *= 10.0; ch = getchar(); } return x * f; }
void writed(ld x){ write(ll(x)); if(x < 0) x = -x; x -= ll(x); if(x == 0){ return; } putchar('.'); while(x != 0){ x *= 10; putchar(ll(x) + '0'); x -= ll(x); } }

const ll N = 1e5 + 3, M = 2e6 + 3, mod = 1e9 + 7, INF = -1e18;
int n;
ll nb;
string s;

struct P{
    ll shu_zi;
    char yun_suan_fu;
    ll you_xian_ji;
}a[M];
ll ac, you_xian_ji;

ll t;
P st[M];

ll c(ll a, ll b, char c){
    if(c == '+') return (a + b) % mod;
    if(c == '-') return (a - b + mod) % mod;
    return a * b % mod;
}

int main(){

    cin >> s;
    s = ' ' + s;
    n = s.size() - 1;

    for(int i = 1; i <= n; i ++){
        if('0' <= s[i] && s[i] <= '9') nb = nb * 10 + s[i] - '0';
        else{
            if(s[i] == '(') you_xian_ji += 3;
            if(s[i] == ')') you_xian_ji -= 3;
            if(nb) ac ++, a[ac] = {nb, ' ', INF};
            if(s[i] != '(' && s[i] != ')') ac ++, a[ac] = {0, s[i], you_xian_ji + (s[i] == '*')};
            nb = 0;
        }
    }
    if(nb) ac ++, a[ac] = {nb, ' ', INF};


    for(int i = 1; i <= ac; i ++){
        while(a[i].yun_suan_fu != ' ' && t >= 3 && st[t - 1].you_xian_ji >= a[i].you_xian_ji){
            if(st[t - 1].yun_suan_fu == '+'){
                nb = (st[t - 2].shu_zi + st[t].shu_zi) % mod;
            }
            if(st[t - 1].yun_suan_fu == '-'){
                nb = (st[t - 2].shu_zi - st[t].shu_zi + mod) % mod;
            }
            if(st[t - 1].yun_suan_fu == '*'){
                nb = st[t - 2].shu_zi * st[t].shu_zi % mod;
            }
            st[t - 2] = {nb, ' ', INF};
            t -= 2;
        }
        t ++, st[t] = a[i];
    }

    while(t >= 3){
        if(st[t - 1].yun_suan_fu == '+'){
            nb = (st[t - 2].shu_zi + st[t].shu_zi) % mod;
        }
        if(st[t - 1].yun_suan_fu == '-'){
            nb = (st[t - 2].shu_zi - st[t].shu_zi + mod) % mod;
        }
        if(st[t - 1].yun_suan_fu == '*'){
            nb = st[t - 2].shu_zi * st[t].shu_zi % mod;
        }
        st[t - 2] = {nb, ' ', INF};
        t -= 2;
    }

    cout << st[1].shu_zi;

    return 0;
}