社区讨论

蒟蒻全RE!求救

P1054[NOIP 2005 提高组] 等价表达式参与者 3已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@lo2b3xet
此快照首次捕获于
2023/10/23 10:56
2 年前
此快照最后确认于
2023/11/03 11:07
2 年前
查看原帖
CPP
#include<bits/stdc++.h>
#include<stack>
#include<string>
using namespace std;
typedef unsigned long long ull;
string c,d;
int n;
stack <ull> s1;
stack <char> s2; 
ull suan(ull x,ull y,char f){
	if(f=='+') return x+y;
	if(f=='-') return x-y;
	if(f=='*') return x*y;
	if(f=='^'){
		ull res=1;
		for(ull i=1;i<=y;i++)
			res*=x;
		return res;
	}
}
int pri(char f){
	if(f=='+'||f=='-') return 1;
	if(f=='*') return 2;
	if(f=='^') return 3;
	return 4;
}
ull fun(string ch,ull u){
	while(!s1.empty()) s1.pop();
	while(!s2.empty()) s2.pop();
	ull 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+(ull)(ch[i]-'0');
		}else{
			if(sum!=1e18+5) s1.push(sum);
			sum=1e18+5;
			if(ch[i]==')'){
				while(!s2.empty()&&s2.top()!='('){
					ull x=s1.top();s1.pop();
					ull y=s1.top();s1.pop();
					s1.push(suan(y,x,s2.top()));
					s2.pop();
				}
				s2.pop();
			}else{
				while(!s2.empty()&&s2.top()!='('&&pri(s2.top())>=pri(ch[i])){
					ull x=s1.top();s1.pop();
					ull y=s1.top();s1.pop();
					s1.push(suan(y,x,s2.top()));
					s2.pop();
				}
				s2.push(ch[i]);
			}
		}
	}
	while(!s2.empty()){
		ull x=s1.top();s1.pop();
		ull 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=5;j<=10;j++){
			if(fun(c,(ull)j)!=fun(d,(ull)j)){
				same=false;
				break;
			}
		}
		if(same) printf("%c",'A'+i-1);
	}
	return 0;
} 
求神犇!!!ORZ

回复

3 条回复,欢迎继续交流。

正在加载回复...