坐标:P1054 [NOIP2005 提高组] 等价表达式 本人太弱,一直 RE !求调代码:评测记录
RE CODE
#include<bits/stdc++.h>
#include<stack>
#include<string>
using namespace std;
typedef long long ll;
const ll P=1e9+7;
string c,d;
int n;
stack <ll> s1;
stack <char> s2;
ll suan(ll x,ll y,char f){
if(f=='+') return (x+y)%P;
if(f=='-') return (x-y+P)%P;
if(f=='*') return x*y%P;
if(f=='^'){
ll res=1;
for(ll i=1;i<=y;i++)
res*=x,res%=P;
return res;
}
}
int pri(char f){
if(f=='+'||f=='-') return 1;
if(f=='*') return 2;
if(f=='^') return 3;
return 4;
}
ll fun(string ch,ll u){
while(!s1.empty()) s1.pop();
while(!s2.empty()) s2.pop();
ll sum=1e18+5;
for(int i=0;i<ch.length();i++){
if(ch[i]==' ') continue;
if(ch[i]=='a') sum=u;
else if(ch[i]>='0'&&ch[i]<='9'){
if(sum==1e18+5) sum=0;
sum=sum*10+(ll)(ch[i]-'0');
sum%=P;
}else{
if(sum!=1e18+5) s1.push(sum);
sum=1e18+5;
if(ch[i]==')'){
while(!s2.empty()&&s2.top()!='('){
if(s1.size()<2) break;
ll x=s1.top();s1.pop();
ll y=s1.top();s1.pop();
s1.push(suan(y,x,s2.top()));
s2.pop();
}
if(!s2.empty()) s2.pop();
}else{
while(!s2.empty()&&s2.top()!='('&&pri(s2.top())>=pri(ch[i])){
if(s1.size()<2) break;
ll x=s1.top();s1.pop();
ll y=s1.top();s1.pop();
s1.push(suan(y,x,s2.top()));
s2.pop();
}
s2.push(ch[i]);
}
}
}
while(!s2.empty()){
if(s1.size()<2) break;
ll x=s1.top();s1.pop();
ll y=s1.top();s1.pop();
s1.push(suan(y,x,s2.top()));
s2.pop();
}
return s1.top();
}
int main(){
// freopen("P1054_1.in","r",stdin);
// freopen("P1054_my.out","w",stdout);
getline(cin,c);
c='('+c+')';
scanf("%d",&n);
getchar();
for(int i=1;i<=n;i++){
getline(cin,d);
d='('+d+')';
bool same=true;
for(int j=1;j<=10;j++){
if(fun(c,(ll)j)!=fun(d,(ll)j)){
same=false;
break;
}
}
if(same) putchar('A'+i-1);
}
printf("\n");
return 0;
}