# NOIP 2005 等价表达式 题解

#include<bits/stdc++.h>
using namespace std;
const int mod=10007;
char w[55],c[55];
int a[100],t1,t2,x,n;
long long ans[5],c1[55],c2[55],c3[55];
int power(int x,int y){
int tmp=1;
while(y){
if(y&1) tmp=(tmp*x)%mod;
x=(x*x)%mod;
y>>=1;
}
return tmp%mod;
}
void clu(){
int m1=c1[t2],n1=c1[t2-1],m2=c2[t2],n2=c2[t2-1],m3=c3[t2],n3=c3[--t2];
switch(w[t1--]){
case '+':c1[t2]=(n1+m1+mod)%mod; c2[t2]=(n2+m2+mod)%mod; c3[t2]=(n3+m3+mod)%mod; break;
case '-':c1[t2]=(n1-m1+mod)%mod; c2[t2]=(n2-m2+mod)%mod; c3[t2]=(n3-m3+mod)%mod; break;
case '*':c1[t2]=(n1*m1+mod)%mod; c2[t2]=(n2*m2+mod)%mod; c3[t2]=(n3*m3+mod)%mod; break;
case '^':c1[t2]=power(n1,m1); c2[t2]=power(n2,m2); c3[t2]=power(n3,m3); break;
}
}
void js(){
x=0;
for(int i=0;i<strlen(c);i++){
if(c[i]=='a') {c1[++t2]=3;c2[t2]=7;c3[t2]=13;}
else if(isdigit(c[i])) x=x*10+c[i]-'0';
else{
if(x!=0){c1[++t2]=x;c2[t2]=x;c3[t2]=x;x=0;}
if(a[c[i]]==0)continue;
if(c[i]=='('||t1==0&&c[i]!=')') w[++t1]=c[i];
else{
if(a[c[i]]<a[w[t1]]){
if(c[i]!=')') w[++t1]=c[i];
else{
while(w[t1]!='('&&t1) clu();
if(w[t1]=='(')t1--;
}
}
else{
while(a[c[i]]>=a[w[t1]]&&t1) clu();
if(c[i]!=')') w[++t1]=c[i];
}
}
}
}
if(x!=0){c1[++t2]=x;c2[t2]=x;c3[t2]=x;x=0;}
while (t1)clu();
}
int main(){
a['(']=5;a[')']=1;a['^']=2;a['*']=a['/']=3;a['+']=a['-']=4;
gets(c);
scanf("%d",&n);
js();
gets(c);
ans[1]=c1[1];ans[2]=c2[1];ans[3]=c3[1];
for(int i=1;i<=n;++i){
t1=t2=0;gets(c);js();
if(ans[1]==c1[1]%mod&&ans[2]==c2[1]%mod&&ans[3]==c3[1]%mod)
printf("%c",i+64);
}
return 0;
}

posted @ 2019-08-27 10:16  End_donkey  阅读(200)  评论(0编辑  收藏  举报