1WA+5RE
查看原帖
1WA+5RE
1191031
majingxuan123楼主2024/4/15 20:08
#include<iostream>
using namespace std;
string s;
int n,x,y,mod=1e9+7;	
int m,a[105];
int top1=0,top2=0,len=0,i=0;
long long ans;
char c[105];
string read(){
    string s;
    char ch=getchar();
    while(ch=='\n'||ch=='\r'||ch==' ')ch=getchar();
    while(ch!='\n'&&ch!='\r'){
        if(ch!=' ')s+=ch;
        ch=getchar();
    }
    return s;
}
long long pow(int x,int y){
	if(y==0)return 1;
	if(y&1)return pow(x,y-1)%mod*x%mod;
	long long temp=pow(x,y>>1)%mod;
	return temp*temp;
}
long long op(int a,int b,char c){
	if(c=='+')return a+b;
	if(c=='-')return b-a+mod;
	if(c=='*')return a*b;
	if(c=='/')return b/a;
	if(c=='^')return pow(b,a);
}
bool cmp(char a,char b){
	if(a=='(')return 1; 
	int d[100];
	d['^']=4;
	d['*']=d['/']=3;
	d['+']=d['-']=2;
	d['(']=1;
	return d[a]>d[b];
}
int f(string s){
	top1=0,top2=0,i=0;
	len=s.size();
	while(i<len){
		if(isdigit(s[i])){
			m=0;
			while(isdigit(s[i])){
				m=m*10+s[i]-'0';
				i++;
			} 
			a[++top1]=m;
		}
		else if(s[i]=='a'){
			a[++top1]=1111;
			i++;
		}
		else{
			if(s[i]==')'){
				while(c[top2]!='('){
					ans=0;
					ans=op(a[top1],a[top1-1],c[top2])%mod;
					top1--,top2--;
					a[top1]=ans;
				}
				top2--;
			}
			else{
				if(top2==0||cmp(s[i],c[top2]))c[++top2]=s[i];
				else{
					while(top2>0&&!cmp(s[i],c[top2])){
						ans=0;
						ans=op(a[top1],a[top1-1],c[top2])%mod;
						top1--,top2--;
						a[top1]=ans;
					}
					c[++top2]=s[i];
				}
			}
			i++;
		}
	}
	while(top2>0){
		ans=0;
		ans=op(a[top1],a[top1-1],c[top2])%mod;
		top1--,top2--;
		a[top1]=ans;
	}
	return a[1];
}
int main(){
	s=read();
	cin>>n;getchar();
	x=f(s);
	for(int i=0;i<n;i++){
		s=read();
		y=f(s);
		if(x==y)cout<<char('A'+i);
	}
	return 0;
}

DALAO求助

记录

2024/4/15 20:08
加载中...