记录编号 121415 评测结果 AAAAAAAAAA
题目名称 [NOIP 2005]等价表达式 最终得分 100
用户昵称 Gravatarslyrabbit 是否通过 通过
代码语言 C++ 运行时间 0.004 s
提交时间 2014-09-19 20:01:38 内存使用 0.28 MiB
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
long long MAX=2147483647/2;
stack<long long>sly_num;
stack<char>sly_sym;
string a,b;
int n,sym[225];
char com[7][7]={
    	{'>','>','>','>','<','>','<'},
	    {'>','>','>','>','<','>','<'},
    	{'<','<','>','>','<','>','<'},
	    {'<','<','<','>','<','>','<'},
    	{'<','<','<','<','<','=','<'},
	    {'>','>','>','>','=','>','<'},
		{'>','>','>','>','>','>','='}};
void init()
{
	sym['+']=0;sym['-']=1;sym['*']=2;sym['^']=3;sym['(']=4;sym[')']=5;sym['#']=6;
}
long long mul(long long x,long long y)
{
	long long ans=1,i;
    x%=MAX;
    for(i=1;i<=y;i++){
        ans%=MAX;
        ans*=x;
        ans%=MAX;
    }
    return ans;
}
long long cou(long long m,char t,long n)
{
	switch(t)
	{
	case'+':return (m+n)%MAX;break;
	case'-':return (m-n%MAX);break;
	case'*':m%=MAX;n%=MAX;return (m*n)%MAX;break;
	case'^':m%=MAX;return mul(m,n)%MAX;break;
	}
}
long long result(string p,int x)
{
	sly_sym.push('#');
	if(!sly_num.empty())
		sly_num.pop();
	for(int i=0;;i++)
	{
		if(i>=p.length()&&sly_sym.top()!='#')
			break;
		if(p[i]==' ')
			continue;
		else
		{
			if((p[i]>='0'&&p[i]<='9')||(p[i]=='a'))
			{
				if(p[i]=='a')
					sly_num.push(x);
				else
				{
					long long woqu=p[i]-'0';
					for(;;i++)
					{
						if(!(p[i+1]>='0'&&p[i+1]<='9'))
							break;
						woqu=woqu*10+p[i+1]-'0';
					}
				    sly_num.push(woqu);
				}
			}
			else
			{
				if((p[i]=='-'&&p[i-1]=='(')||(p[i]=='-'&&i==0))
				{
					sly_num.push(-1);
					p[i]='*';
					i--;
					continue;
				}
				if(sly_sym.top()=='#')
				{
					sly_sym.push(p[i]);
					continue;
				}
				char temp=com[sym[p[i]]][sym[sly_sym.top()]];
				if(temp=='<')
				{
					sly_sym.push(p[i]);
				}
				if(temp=='=')
				{
					sly_sym.pop();
				}
				if(temp=='>')
				{
					i--;
					char data=sly_sym.top();sly_sym.pop();
					long long s1=sly_num.top();sly_num.pop();
					long long s2=sly_num.top();sly_num.pop();
					s1%=MAX;
					s2%=MAX;
					sly_num.push(cou(s2,data,s1));
				}
			}
		}
	}
	return sly_num.top();
}
int main()
{
	freopen("equal.in","r",stdin);
	freopen("equal.out","w",stdout);
	init();
	getline(cin,a);
	a+='#';
	cin>>n;
	getline(cin,b);
	int temp=n;
	while(n--)
	{
		bool t=true;
		getline(cin,b);
		b+='#';
		for(int i=1;i<=3;i++)
		{
			long long ans1=result(a,i);
			long long ans2=result(b,i);
			if(ans1!=ans2)
			{
				t=false;
				break;
			}
		}
		if(t)
			cout<<(char)('A'+temp-n-1);
	}
	return 0;
}