题解:P1054 [NOIP 2005 提高组] 等价表达式

· · 题解

说在前面

这题非常坑,包括但不限于:爆 long long、括号不匹配……

前置芝士:表达式求值。

题目分析

我们拿到题一看:判断两个表达式等不等价。那么我们使用 hash 思想:可以把 a 带入一个值,然后计算两个表达式的值看相不相等即可。但是,这样做存在一个问题:就是原本两个不等价的表达式等价了。举个例子:1 和 a - 1,当 a = 2 时这两个式子是等价的,实际并不等价。所以我们可以代入多个值,例如 1 到 10,其中只要有一个不匹配,那么这两个式子不等价。

细节问题

  1. 本题非常友(e)好(xin),输入的末尾会有 \r,直接使用 getline 会 RE,我当时条了好久,后来参考了其他题解才知道这个问题。所以我们这里使用循环读入字符串:
char c = getchar();
string a = "";
while (c == '\n' || c == '\r') {
    c = getchar();
}
while (c != '\n' && c != '\r') {
    c = getchar();
    a += c;
}
  1. 本题的数据会爆 long long,可以使用一个模数,把值对它取模。(这样也算加强 hash 了好吧,虽然不知道会不会有冲突)
  2. 本题有括号不匹配的情况,所以要去除多余的右括号。
void init(string & y) {
    int l = 0, r = 0;
    for (int i = 0; i < (int)y.size(); i++) {
        if (y[i] == '(') {
            l++;
        } else if (y[i] == ')') {
            r++;
        }
        if (l < r) {
            y.erase(i);
            r--;
        }
    }
}

然后我们使用栈将中缀表达式转为后缀表达式,再使用另一个栈进行计算即可。

完整代码

#include <bits/stdc++.h>

#define int long long

using namespace std;

typedef long long ll;

const int N = 1000 + 5, mod = 998244353;

map<char, int> pty;

string a, tmp;

int n;

int p2[N], sum[N];

stack<char> s;

stack<int> s1;

void init(string & y) {
    int l = 0, r = 0;
    for (int i = 0; i < (int)y.size(); i++) {
        if (y[i] == '(') {
            l++;
        } else if (y[i] == ')') {
            r++;
        }
        if (l < r) {
            y.erase(i);
            r--;
        }
    }
}

int qpow(int a, int b) {
    if (b == 0ll) {
        return 1ll;
    }
    if (b == 1ll) {
        return (ll)a;
    }
    int ans;
    if (b % 2ll) {
        ans = qpow(a, b / 2ll) % mod;
        ans = (ans % mod) * (ans % mod) % mod;
        ans = (ans % mod) * (a % mod) % mod;
    } else {
        ans = qpow(a, b / 2ll) % mod;
        ans = (ans % mod) * (ans % mod) % mod;
    }
    return ans;
}

int solve(string x, int y) {
    for (int i = 0; i < (int)x.length(); i++) {
        if (x[i] == ' ') continue;
        if (x[i] == 'a') {
            tmp += to_string(y) + ".";
            continue;
        }
        if (x[i] >= '0' && x[i] <= '9') {
            while (i < (int)x.length() && x[i] >= '0' && x[i] <= '9') {
                tmp += x[i];
                i++;
            }
            i--;
            tmp += ".";
            continue;
        } else {
            if (x[i] == '(') {
                s.push(x[i]);
            } else if (x[i] == ')') {
                while (!s.empty() && s.top() != '(') {
                    tmp += s.top();
                    s.pop();
                }
                s.pop();
            } else {
                while (!s.empty() && pty[x[i]] <= pty[s.top()]) {
                    tmp += s.top();
                    s.pop();
                }
                s.push(x[i]);
            }
        }
    }
    while (!s.empty()) {
        tmp += s.top();
        s.pop();
    }
    int p1 = 0ll;
    for (int i = 0; i < (int)tmp.length(); i++) {
        if (tmp[i] >= '0' && tmp[i] <= '9') {
            while (tmp[i] != '.' && tmp[i] >= '0' && tmp[i] <= '9') {
                p1 = (ll)((p1 << 1ll) + (p1 << 3ll) + (tmp[i] - 48ll));
                i++;
            }
            s1.push(p1);
            p1 = 0ll;
        } else if (tmp[i] != '.') {
            int a = s1.top();
            s1.pop();
            int b = s1.top();
            s1.pop();
            if (tmp[i] == '+') {
                s1.push((((ll)b + (ll)a) % mod + mod) % mod);
            } else if (tmp[i] == '-') {
                s1.push(((ll)b - (ll)a + mod) % mod);
            } else if (tmp[i] == '*') {
                s1.push(((ll)b * (ll)a % mod + mod) % mod);
            } else if (tmp[i] == '^') {
                s1.push((qpow((ll)b, (ll)a) + mod) % mod);
            }
        }
    }
    int ans = s1.top() % mod;
    s1.pop();
    return ans % mod;
}

signed main() {
    pty['('] = -100;
    pty['+'] = pty['-'] = 1;
    pty['*'] = pty['/'] = 2;
    pty['^'] = 3;
    char c = getchar();
    while (c != '\n' && c != '\r') {
        a += c;
        c = getchar();
    }
    init(a);
    for (int i = 1; i <= 10; i++) {
        p2[i] = solve(a, (ll)i) % mod;
    }
    cin >> n;
    string res = "";
    getchar();
    for (int i = 1; i <= n; i++) {
        string str = "";
        c = getchar();
        while (c == '\n' || c == '\r') {
            c = getchar();
        }
        while (c != '\n' && c != '\r') {
            str += c;
            c = getchar();
        }
        init(str);
        for (int j = 1; j <= 10; j++) {
            sum[j] = solve(str, (ll)j) % mod;
        }
        bool flag = true;
        for (int j = 1; j <= 10; j++) {
            if (p2[j] != sum[j]) {
                flag = false;
                break;
            }
        }
        if (flag) {
            res += 'A' + i - 1;
        }
    }
    cout << res;
    return 0;
}

说在后面

若代码、思路讲解有误,欢迎提出!