40分求助(P4029)
查看原帖
40分求助(P4029)
497817
羊摆摇楼主2022/10/10 14:15

rt

#include <bits/stdc++.h>
using namespace std;

#define LL long long
typedef const int Int;

LL cnt[30];

int T,m;

string s;

bool zj,sl;

bool ischar(const char &a){return 'A'<=a&&a<='Z';}
bool isnumb(const char &a){return '0'<=a&&a<='9';}
int charturnint(const char &a){return a-'A'+1;}

LL numbturnint(const int &x){
	LL ret=0;
	string a;
	LL p=1;
	for(int k=x;isnumb(s[k]);k++)a+=s[k];
	for(int k=a.size()-1;k>=0;k--){
		ret+=(a[k]-'0')*p;
		p*=10;
	}
	return ret;
}



void init(int x){
	switch(x){
		case 0:
			zj=true,sl=true;
			break;
		case 1:
			zj=false,sl=true;
			break;
		case 2:
			zj=true,sl=false;
			break;
		case 3:
			zj=false,sl=false;
			break;
	}
}

bool check(string s){
	bool oth=false,ques;
	for(int k=0;k<s.size();k++){
		if(s[k]=='+')continue;
		if(ischar(s[k])){
			if(k+1<s.size()&&isnumb(s[k+1]))cnt[charturnint(s[k])]+=(oth?-1:1)*numbturnint(k+1);
			else cnt[charturnint(s[k])]+=(oth?-1:1);
		}
		else if(s[k]=='=')oth=true;
		else if(s[k]=='?')ques=oth;
	}
	bool iszero=true;
	for(int k=1;k<=26;k++){
		if((ques?cnt[k]<0:cnt[k]>0))return false;
		if(cnt[k])iszero=false;
	}
	if(iszero)return false;
	return true;
}

int main(){
	scanf("%d%d",&T,&m);
//	init(m);
	while(T--){
		cin>>s;
		if(!check(s))printf("No Solution");
		else for(int k=1;k<=26;k++)if(cnt[k]){
			putchar(k+'A'-1);
			if(cnt[k]!=1&&cnt[k]!=-1)printf("%lld",abs(cnt[k]));
		}
//		cout<<endl;
//		for(int k=1;k<=26;k++)cout<<cnt[k]<<" ";
		putchar('\n');
		memset(cnt,0,sizeof(cnt));
	}
	return 0;
} 










2022/10/10 14:15
加载中...