提交时间:2026-07-17 11:15:10

运行 ID: 92658

#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; }