noip 2005 等价表达式

/*
开始想的是 维护a的每个指数的系数
然而不好办 然而还有^10^10^10这种数据
特殊值带入吧 多搞几个素数 接下来就是玄学的事了
给a赋值之后 就是简单地表达式求值
虽然思路简单 但是字符串一向很恶心、、 数据括号有问题。。。。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 255
#define mod 10007
using namespace std;
int n,a,top1,top2,l,li,s1[maxn],s2[maxn];
int ans[]={,,,,,};
int target[];
int order[];
char s[maxn],si[maxn],c;
int Mi(int x,int y)
{
int ans=x;
for(int i=;i<=y;i++)
ans=(ans*x)%mod;
return ans;
}
int Go(int x,int y,char z)
{
if(z==)return (x-y+mod)%mod;
if(z==)return (x+y)%mod;
if(z==)return (x*y)%mod;
if(z==)return Mi(x,y);
}
int main()
{
order[]=order[]=;
order[]=;order[]=;
gets(si);li=strlen(si+);l=;
for(int i=;i<=li;i++)
if(si[i]==' ')continue;
else s[++l]=si[i];
s[]='(';s[++l]=')';
for(int k=;k<=;k++)
{
a=ans[k];int i=;top1=top2=;
memset(s1,,sizeof(s1));
memset(s2,,sizeof(s2));
while(i<=l)
{
if(s[i]=='a')
{
if(top1>=&&s[i-]>=''&&s[i-]<='')
{
int tmp=s1[top1];
s1[top1]=a*tmp%mod;
}
else top1=top1+,s1[top1]=a;
}
if(s[i]>=''&&s[i]<='')
{
int x=;
while(s[i]>=''&&s[i]<=''){x=x*+s[i]-'';i++;}
top1=top1+;s1[top1]=x;i=i-;
}
else
{
if(s[i]=='(')top2=top2+,s2[top2]=s[i];
if(s[i]=='^')
{
int x=;i=i+;int r=s1[top1];
while(s[i]>=''&&s[i]<=''){x=x*+s[i]-'';i++;}
s1[top1]=Go(r,x,'^');i=i-;
}
if(s[i]=='+'||s[i]=='-'||s[i]=='*')
{
if(order[s[i]]<=order[s2[top2]])
{
while(order[s[i]]<=order[s2[top2]])
{
int r1=s1[top1];top1=top1-;
int r2=s1[top1];top1=top1-;
int r3=Go(r2,r1,s2[top2]);
top2=top2-;top1=top1+;s1[top1]=r3;
}
top2=top2+;s2[top2]=s[i];
}
else if(order[s[i]]>order[s2[top2]])top2=top2+,s2[top2]=s[i];
}
if(s[i]==')')
{
while(s2[top2]!='('&&top2)
{
int r1=s1[top1];top1=top1-;
int r2=s1[top1];top1=top1-;
int r3=Go(r2,r1,s2[top2]);
top2=top2-;top1=top1+;s1[top1]=r3;
}
top2=top2-;
}
}
i=i+;
}
target[k]=s1[];
}
scanf("%d",&n);c=getchar();
for(int p=;p<=n;p++)
{
li=;memset(si,,sizeof(si));
gets(si);li=strlen(si+);l=;
for(int i=;i<=li;i++)
if(si[i]==' ')continue;
else s[++l]=si[i];
s[]='(';s[++l]=')';int sum=;
for(int k=;k<=;k++)
{
a=ans[k];int i=;top1=top2=;
memset(s1,,sizeof(s1));
memset(s2,,sizeof(s2));
while(i<=l)
{
if(s[i]=='a')
{
if(top1>=&&s[i-]>=''&&s[i-]<='')
{
int tmp=s1[top1];
s1[top1]=a*tmp%mod;
}
else top1=top1+,s1[top1]=a;
}
if(s[i]>=''&&s[i]<='')
{
int x=;
while(s[i]>=''&&s[i]<=''){x=x*+s[i]-'';i++;}
top1=top1+;s1[top1]=x;i=i-;
}
else
{
if(s[i]=='(')top2=top2+,s2[top2]=s[i];
if(s[i]=='^')
{
int x=;i=i+;int r=s1[top1];
while(s[i]>=''&&s[i]<=''){x=x*+s[i]-'';i++;}
s1[top1]=Go(r,x,'^');i=i-;
}
if(s[i]=='+'||s[i]=='-'||s[i]=='*')
{
if(order[s[i]]<=order[s2[top2]])
{
while(order[s[i]]<=order[s2[top2]])
{
int r1=s1[top1];top1=top1-;
int r2=s1[top1];top1=top1-;
int r3=Go(r2,r1,s2[top2]);
top2=top2-;top1=top1+;s1[top1]=r3;
}
top2=top2+;s2[top2]=s[i];
}
if(order[s[i]]>order[s2[top2]])top2=top2+,s2[top2]=s[i];
}
if(s[i]==')')
{
while(s2[top2]!='('&&top2)
{
int r1=s1[top1];top1=top1-;
int r2=s1[top1];top1=top1-;
int r3=Go(r2,r1,s2[top2]);
top2=top2-;top1=top1+;s1[top1]=r3;
}
top2=top2-;
}
}
i=i+;
}
if(s1[]==target[k])sum++;
}
if(sum==)printf("%c",'A'-+p);
}
return ;
}
上一篇:vim 删除临时文件


下一篇:UVA 1613 K-Graph Oddity