求助!本地下载数据运行正确,提交全WA
查看原帖
求助!本地下载数据运行正确,提交全WA
233815
zhjzhmh楼主2022/9/21 21:54

RT

#include<bits/stdc++.h>
#define int long long
#define mod 998244353
using namespace std;
stack<char> s1;
stack<int>  s2;
int x,y,yxj[130],i,pd,a[30],n,bo;
string st,s;
char ch='A'-1;
int pow(int x,int y)
{
	int p=1;
	while(y--) p*=x,p%=mod;
	return p;
}
void cal()
{
	y=s2.top();s2.pop();x=s2.top();s2.pop();
	if(s1.top()=='+') s2.push((x+y)%mod);
	if(s1.top()=='-') s2.push((x-y)%mod);
	if(s1.top()=='^') s2.push(pow(x,y)%mod);
	if(s1.top()=='*') s2.push((x*y)%mod);
	s1.pop();
}
signed main()
{
	getline(cin,s);
	for(int i=0;i<=s.size()-1;i++) if(s[i]!=' ') st+=s[i];
	if(st[st.size()-1]=='\n'||st[st.size()-1]=='\r\n') st.erase(st.size()-1,1);
	yxj[')']=0;yxj['+']=1;yxj['*']=2;yxj['-']=1;yxj['^']=3; 
	st=st+')';
	for(int j=0;j<=20;j++)
	{
		while(!s1.empty()) s1.pop();
		while(!s2.empty()) s2.pop();
		s1.push('(');
		i=0;
		while(i<st.size())
		{
			x=0;
			pd=0;
			while(st[i]>='0'&&st[i]<='9'&&i<=st.size()-1)
			{
				x=x*10+st[i]-'0';
				i++;pd=1;
			}
			if(st[i]=='a') x=j,pd=1,i++;
			if(pd) s2.push(x%mod);
			if(st[i]==')')
			{
				while(s1.top()!='('&&!s1.empty()) cal();
				s1.pop();
				i++;
			}
			else if(st[i]=='+'||st[i]=='*'||st[i]=='-'||st[i]=='^')
			{
				while(!s1.empty()&&yxj[s1.top()]>=yxj[st[i]]) cal();
				s1.push(st[i]);
				i++;
			}
			else s1.push('('),i++;
		}
		a[j]=s2.top();
	}
	cin>>n;getline(cin,s);
	while(n--)
	{
		getline(cin,s);st="";
		for(int i=0;i<=s.size()-1;i++) if(s[i]!=' ') st+=s[i];ch++;bo=1;
		if(st[st.size()-1]=='\n'||st[st.size()-1]=='\r\n') st.erase(st.size()-1,1);st+=')';
		for(int j=0;j<=20;j++)
		{
			while(!s1.empty()) s1.pop();
			while(!s2.empty()) s2.pop();
			s1.push('(');
			i=0;
			while(i<st.size())
			{
				x=0;
				pd=0;
				while(st[i]>='0'&&st[i]<='9'&&i<=st.size()-1)
				{
					x=x*10+st[i]-'0';
					i++;pd=1;
				}
				if(st[i]=='a') x=j,pd=1,i++;
				if(pd) s2.push(x%mod);
				if(st[i]==')')
				{
					while(s1.top()!='('&&!s1.empty()) cal();
					s1.pop();
					i++;
				}
				else if(st[i]=='+'||st[i]=='*'||st[i]=='-'||st[i]=='^')
				{
					while(!s1.empty()&&yxj[s1.top()]>=yxj[st[i]]) cal();
					s1.push(st[i]);
					i++;
				}
				else s1.push('('),i++;
			}
			if(s2.top()!=a[j]) {bo=0;break;}
		}
		if(bo) cout<<char(ch);
	}
} 
2022/9/21 21:54
加载中...