比赛 暑假综合模拟2 评测结果 AAAAAAAAAA
题目名称 等价表达式 最终得分 100
用户昵称 CloudTower 运行时间 0.051 s
代码语言 C++ 内存使用 0.25 MiB
提交时间 2018-08-05 18:53:43
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
stack<long long> shu;
stack<char> fu;
char s[60],a;
long long test1=0;
long long test2=1007;
long long n;
long long f(char x)
{
	if(x=='+'||x=='-')return 1;
	if(x=='*')return 2;
	if(x=='^')return 3;
	if(x=='(')return 0;
}
void js(stack<long long>&s1,stack<char>&s2)
{
	long long x,y,ans=1;
	char ch;
	x=s1.top();s1.pop();
	y=s1.top();s1.pop();
	ch=s2.top();s2.pop();
	if(ch=='+')s1.push(x+y);
	if(ch=='-')s1.push(y-x);
	if(ch=='*')s1.push(x*y);
	if(ch=='^')
	{
		for(long long i=1;i<=x;i++)
		{
			ans*=y;
		}
		s1.push(ans);
	}
}
long long work(long long x)
{
    bool flag=0;
	long long ans0=0;
	long long temp=0,ans=1;
	long long len=strlen(s);
	for(long long i=0;i<len;i++)
	{
		if(s[i]=='\n')continue;
		if(s[i]==' ')continue;
		if(s[i]=='a')shu.push(x);
		else if(s[i]>='0'&&s[i]<='9')
		{
			temp=temp*10+s[i]-'0';
			flag=1;
		}
		else {
			if(flag)shu.push(temp),flag=0,temp=0;
			if(s[i]=='('){fu.push(s[i]);continue;}
			if(s[i]==')')
			{
				while(fu.top()!='(')js(shu,fu);
				fu.pop();
				continue;
			}
			if(fu.empty())fu.push(s[i]);
			else{
				while(!fu.empty()&&f(fu.top())>=f(s[i]))js(shu,fu);
				fu.push(s[i]);
			}
		}
	}
	if(flag){shu.push(temp);flag=0;temp=0;}
	while(!fu.empty())js(shu,fu);
	ans0=shu.top();
//	cout<<ans0<<" ";
	while(!shu.empty())shu.pop();
    while(!fu.empty())fu.pop();
	return ans0;
}
int main()
{
	freopen("equal.in","r",stdin);
	freopen("equal.out","w",stdout);
	gets(s);
	long long ans1=work(test1);
	long long ans2=work(test2);
	cin>>n;
	gets(s);
	for(long long i=1;i<=n;i++)
	{
		gets(s);
//		cout<<s<<endl;
//		long long ans3=work(test1),ans4=work(test2);
//		cout<<ans3<<" "<<ans4;
	//	if(ans1==ans3&&ans2==ans4);cout<<(char)i+'A'-1;
		if(ans1==work(test1)&&ans2==work(test2)){a=i+'A'-1;cout<<(char)a;}
	}
	fclose(stdin);fclose(stdout);
	return 0;
}