全测试点RE求调
查看原帖
全测试点RE求调
405111
HeHNCu楼主2024/10/8 22:17

代码:

#include<algorithm>
#include<unordered_map>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<time.h>
#include<stack>
#include<map>
#define elif else if //写python写的 
using namespace std;
const int mod = 13331 ;
stack<int> num;
stack<char> ops;
unordered_map<char, int> pri = {{'+', 1}, {'-', 1}, {'*', 2}, {'^', 3}};

int power(int a,int b){
	int ans=1;
	while (b--)ans=ans*a%mod;
	return ans;
}

void eval() {
	int b = num.top();
	num.pop();
	int a = num.top();
	num.pop();
	char c = ops.top();
	ops.pop();
	if (c == '+') num.push((a + b) % mod);
	if (c == '-') num.push(((a - b) % mod + mod) % mod);
	if (c == '*') num.push((a * b) % mod);
	if (c == '^') {
		num.push(power(a,b));
	}
}

int cal(string s, int x) {
	while (num.size()) num.pop();
	while (ops.size()) ops.pop();
	for (int i = 0; i < s.size(); ++ i) {
		if (s[i] == ' ') continue;
		if (s[i] == '(') ops.push(s[i]);
		elif (s[i] == ')') {
			while (ops.size() && ops.top() != '(') eval();
			ops.pop();
		} elif (s[i] >= '0' && s[i] <= '9') {
			int t = 0;
			while (i < s.size() && s[i] >= '0' && s[i] <= '9') t = t*10 + s[i++] - '0';
			num.push(t);
			i = i-1;
		} elif (s[i] == 'a') num.push(x);
		else {
			while (ops.size() && pri[ops.top()] >= pri[s[i]]) eval();
			ops.push(s[i]);
		}
	}
	while (ops.size()) eval();
	return num.top();
}
int main() {
	string s, t;
	int n;
	getline(cin, s);
	cin >> n;
	getchar();
	for (char i = 'A'; i - 'A' < n; ++ i) {
		getline(cin ,t);
		if (cal(s, 1145) == cal(t, 1145)) cout << i;
	}
	return 0;
}
2024/10/8 22:17
加载中...