CCF201912-3

CCF201912-3

70分 样例全部通过 待更正。。。

#include
#include
#include
#include
#include
#include

using namespace std;

typedef struct{
     
	string s;
	int num;
}mystring;
void out(mystring x){
     
	cout<<x.s<<" "<<x.num<<endl;
}
void outString(vector<mystring> e){
     
	for_each(e.begin(),e.end(),out);
	cout<<endl;
}
bool isnumber(char c){
     
	if(c>='0'&&c<='9')
		return true;
	return false;
}
bool isbig(char c){
     
	if(c>='A'&&c<='Z')
		return true;
	return false;
}
bool issmall(char c){
     
	if(c>='a'&&c<='z')
		return true;
	return false;
}
int find(vector<mystring> m,string x){
     
	int len = m.size();
	for(int i=0;i<len;i++){
     
		if(m[i].s==x)
			return i;
	}
	return -1;
}
bool cmp(mystring x1,mystring x2){
     
	if(x1.s[0]-x2.s[0]>0)
		return true;
	else if(x1.s[0]-x2.s[0]==0){
     
		int size1 = x1.s.size();
		int size2 = x2.s.size(); 
		if(size1>size2){
     
			return true;
		} 
		if(x1.s[1]-x2.s[1]>0){
     
			return true;
		}
	}
	return false;
}
int tonumber(string s,int i){
     
	int len = s.length();
	int ans = 0;
	while(i<len&&isnumber(s[i])){
     
		ans = ans*10 + s[i] - '0';
		i++;
	}
	if(ans==0)
		ans = 1;
	return ans;
}
void func(string s,vector<mystring> &ans,int time){
     
	int len = s.length();
	if(isnumber(s[0])){
     
		time = tonumber(s,0);
	}
	for(int i=0;i<len;i++){
     
		if(isbig(s[i])){
     
			mystring t;
			if(i+1<len){
     
				if(issmall(s[i+1])){
     
					t.s.assign(s,i,2);
					int num = tonumber(s,i+2);
					t.num = time*num;
					
				}	
				else if(isnumber(s[i+1])){
     
					t.s.assign(s,i,1);
					int num = tonumber(s,i+1);
					t.num = time*num;
				}
				else{
     
					t.s.assign(s,i,1);
					t.num = time;
				}
			}
			else{
     
				t.s.assign(s,i,1);
				t.num = time;
			}
			int index = find(ans,t.s);
			if(index==-1)
				ans.push_back(t);
			else{
     
				ans[index].num += t.num;
			}
		}
		else if(s[i]=='('){
     
			int num_k = 1;
			for(int j=i+1;j<len;j++){
     
				if(s[j]=='(')
					num_k++;
				else if(s[j]==')'){
     
					num_k--;
					if(num_k==0){
     
						string sub;
						sub.assign(s,i+1,j-i-1);
						int num = tonumber(s,j+1);
						int time_copy = num * time;
						func(sub,ans,time_copy);
						i = j;
					}
				}
					
			}
		}
	}
	
}
void process_expression(vector<string> e,vector<mystring> &ans){
     
	int len = e.size();
	for(int i=0;i<len;i++){
     
		func(e[i],ans,1);
	}
}
void process(){
     
	char tmp[1000];
	vector<string> expression;
	gets(tmp);
	char *sp = strtok(tmp,"=");
	while(sp){
     
		expression.push_back(sp);
		sp = strtok(NULL,"=");
	}
	vector<string> expression1;
	vector<string> expression2;	
	char * tmp1 = (char*)expression[0].c_str();
	char *sp1 = strtok(tmp1,"+");
	while(sp1){
     
		expression1.push_back(sp1);
		sp1 = strtok(NULL,"+");
	}
	char * tmp2 = (char*)expression[1].c_str();
	char *sp2 = strtok(tmp2,"+");
	while(sp2){
     
		expression2.push_back(sp2);
		sp2 = strtok(NULL,"+");
	}
	vector<mystring> ans1;
	process_expression(expression1,ans1);
	vector<mystring> ans2;
	process_expression(expression2,ans2);
	sort(ans1.begin(),ans1.end(),cmp);
	sort(ans2.begin(),ans2.end(),cmp);
	int len1 = ans1.size();
	int len2 = ans2.size();
	if(len1!=len2){
     
		cout<<'N'<<endl;
		return;
	}
	for(int i=0;i<len1;i++){
     
		if(ans1[i].num!=ans2[i].num||ans1[i].s.compare(ans2[i].s)!=0){
     
			cout<<'N'<<endl;
			return;
		}
	}
	cout<<'Y'<<endl;
	return;
}



int main(){
     
	int n;
	cin>>n;
	char tmp = getchar();
	for(int i=0;i<n;i++){
     
		process();
	}
	return 0;
}
``` 

你可能感兴趣的:(CCF)