CCF CSP化学方程式

一年后再次写这题,当时的梦魇额额,但只有90分,调不出最后的错误,留个标记,以后有时间再改改

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cstdlib>
//#define DEBUG
using namespace std;
struct Ele{
	string s;
	int num;
};

enum{LEFT,RIGHT,MIDDLE     
};

vector<Ele> Left,Right;

bool operator==(const Ele& A,const Ele &B)
{
	if(A.s==B.s)
		if(A.num==B.num)
			return true;
	return false ;
}

bool judge()//判断是否相等 
{
	//if(Left.size()!=Right.size())
	//	return false; 
	int len=Left.size();
	#ifdef DEBUG
	for(int i=0;i<len;i++)
		cout<<"left   "<<Left[i].s<<"   "<<Left[i].num<<endl;
	for(int i=0;i<Right.size();i++)
		cout<<"rigth   "<<Right[i].s<<"   "<<Right[i].num<<endl;
		#endif
	for(int i=0;i<len;i++)
	{
		if(find(Right.begin(),Right.end(),Left[i])==Right.end())
		{
			return false ;
		}
	}
	return true;
}
int count_sp(string s)//用于计算初始的分子(+号之间的式子)的系数 
{
	int start=0;
	int end =0;
	while(s[end]>='0'&&s[end]<='9')
	end++ ;
	if(start==end)
	return 1;
	
	string k=s.substr(start,end-start);
	return atoi(k.c_str());
}
void add(string s,int tag,int arg=1)//arg表示个数   添加元素 
{
	if(tag==LEFT)
	{
		int len=Left.size();
		for(int i=0;i<len;i++)
		{
			if(Left[i].s==s)
			{
				Left[i].num+=arg;
				return;
			}	
		}
		Ele k;
		k.s=s;
		k.num=arg;
		Left.push_back(k);
	}
	else
	{
		int len=Right.size();
		for(int i=0;i<len;i++)
		{
			if(Right[i].s==s)
			{
				Right[i].num+=arg;
				return;
			}	
		}
		Ele k;
		k.s=s;
		k.num=arg;
		Right.push_back(k);
	}
}
void count(string s,int tag,int arg=1)//arg表示所有得到的数需要乘的倍数 计算式子 
{

	vector<int> p;
	int num_r=0;
	int len=s.length();
	for(int i=0;i<len;i++)
	{
		if((p.size()!=num_r)&&s[i]!=')'&&s[i]!='(')
			continue;
		if(s[i]>='A'&&s[i]<='Z')
		{
			int l=1;
			if (s[i+1]>='a'&&s[i+1]<='z')
				l++;
			int start=i+l;
			int end=i+l;
			while(s[end]>='0'&&s[end]<='9')
				end++;
			if(start==end)
			{
				string k="";
				if(l==1)
				k+=s[i];
				else 
				k=k+s[i]+s[i+1];
				add(k,tag,arg);
			}
			else
			{
				string s_num=s.substr(start,end-start);
				int num=atoi(s_num.c_str());
				string k="";
				if(l==1)
					k+=s[i];
				else
					k=k+s[i]+s[i+1];
				add(k,tag,num*arg);
			}
		}
		else if(s[i]=='(')
			p.push_back(i);
		else if(s[i]==')')
		{
			num_r++;
			if(num_r!=p.size())
				continue;
			int l=p[0];
			string k=s.substr(l+1,i-l-1);
			#ifdef DEBUG
			cout<<"括号内字符     "<<k<<"   "<<num_r<<"    "<<p.size()<<endl;
			#endif 
			int start=i+1;
			int end =start;
			while(s[end]>='0'&&s[end]<='9')
				end++;
			if(start==end)
			{
				count(k,tag,arg*1*count_sp(k));
			}
			else
			{
				string s_num=s.substr(start,end-start);
				int num=atoi(s_num.c_str());
				count(k,tag,num*arg*count_sp(k));
				  
			}
			p.clear();
			num_r=0;		
		}
		
	}
}


void split(string s,int tag=MIDDLE)//分隔式子 
{
	#ifdef DEBUG
	cout<<s<<endl;
	#endif
	if(tag==MIDDLE)
	{
		int li=s.find('=');
		
		split(s.substr(0,li),LEFT);
		split(s.substr(li+1,s.length()-li-1),RIGHT);
		return; 
	}
	int len=s.length();
	int pos=0;
	for(int i=0;i<len;i++)
	{
		if(s[i]=='+')
		{
			string k=s.substr(pos,i-pos);
			
			#ifdef DEBUG
			cout<<k<<"     元串"<<endl;
			#endif
			count(k,tag,count_sp(k));
			pos=i+1;
		}
	}
	string k=s.substr(pos,s.length()-pos);
		#ifdef DEBUG
		cout<<k<<"    末元串"<<endl; 
		#endif
	count(k,tag,count_sp(k));
}

int main()
{
	string s;
	int n;
	cin>>n;
	while(n--)
	{
		cin>>s;
		split(s);
		if(judge())
			cout<<"Y"<<endl;
		else 
			cout<<"N"<<endl;
		Left.clear();
		Right.clear();
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值