csp 201912-3 化学方程式30分之谜(有偿求解!)

题目如下:
在这里插入图片描述
我采用的堆栈队列来处理的,有空会来写一下注释。
题目里的测试全部试过了,不知道为什么只有30分,有偿求大佬解答!!!

#include<stdio.h>
#include<iostream>
#include<string>
#include<regex>
#include<queue>
#include<stack>
#include <algorithm>
#include<vector>
using namespace std;

typedef struct BITEM{
	string name;
	int numl;
	int numr;
}bitem;
vector<bitem> bitems;

typedef struct BUFF{
	string name;
	int num;
}buffs;

vector<buffs> buff;

char result[101];

int test(string s){
	for(int i=0;i<buff.size() ;i++){
		if(buff[i].name==s)
		return i; 
	}
	return 0;
}

queue<string> le;
queue<string> ri;

void write(string name,int num){
	for(int i=0;i<buff.size() ;i++){
		if(buff[i].name==name)
		{
		buff[i].num+=num;
		return;
		}
	}
	buffs tmp;
	
	tmp.name =name;
	tmp.num=num;
	
	buff.push_back(tmp); 
}


void count(string s){
	int p=0;
	string snum;
	while(s[p]>='0'&&s[p]<='9'){
		snum=snum+s[p];
		p++;
	}
	int num;
	if(p>0) {
	num=stoi(snum);
	s=s.substr(p);
	}
	else num=1;

	stack<int> times;
	times.push(num);
	
	stack<char> its;
	for(int i=0;i<s.size() ;i++){
		its.push(s[i]);
	}
	while(!its.empty()){
		char a=its.top();
		its.pop();
		string local_snum;
		string name;
		int local_num=1;
		bool num_flag=0;
		while(a>='0'&&a<='9'){
			local_snum=local_snum+a;
			a=its.top();
			its.pop();
			num_flag=1;
		}
		if(num_flag) {
			reverse(local_snum.begin(),local_snum.end());
			local_num=stoi(local_snum);
		}
		if(a==')'){
			times.push(times.top()*local_num);
			continue;
		}
		else if(a=='('){
			times.pop();
			continue;
		}
		else if(a>='a'&&a<='z'){
			name+=a;
			name+=its.top();
			its.pop();
			reverse(name.begin() ,name.end() );
		}
		else if(a>='A'&&a<='Z'){
			name=a;
		}
		write(name,local_num*times.top());
	}

}

void move_l(){
	while(buff.size()){
		buffs b=buff.back();
		string name=b.name;
		bool flag=0;
		for(int i=0;i<bitems.size() ;i++){
			if(name==bitems[i].name){
				bitems[i].numl +=b.num ;
				flag=1;
				break;
			}
		}	
		if(!flag){
		bitem tmp;
		tmp.name=b.name;
		tmp.numl =b.num;
		bitems.push_back(tmp);
		}
		buff.pop_back();
	}
}

bool move_r(int i){
	while(buff.size()){
		string name=buff.back().name;
		bool flag=0;
		for(int i=0;i<bitems.size();i++){
			if(name==bitems[i].name){
				bitems[i].numr +=buff.back().num ;
				flag=1;
				break;
			}
		}
		if(!flag){
		result[i]='N';
		return 0;
		}
		buff.pop_back();
	}
	return 1;
}

void handle_l(){
			while(!le.empty()){//逐个处理化学式 
				count(le.front());
				le.pop();
				move_l();
				buff.clear() ;
		}
}

bool handle_r(int i){
			while(!ri.empty()){//逐个处理化学式 
				count(ri.front());
				ri.pop();
				bool flag=move_r(i);
				buff.clear();
				if(flag==0){
					return 0;
				}
		}
		return 1;
		
}

void test_finally(int p){
	for(int i=0;i<bitems.size();i++){
		if(bitems[i].numl !=bitems[i].numr)
		result[p]='N';
	    else result[p]='Y';
	}
}

void get_items(string left,string right){
	
			while(1){
				int pos=left.find('+');
				if(pos!=-1){
				le.push(left.substr(0,pos));
				left=left.substr(pos+1);
			}
			else{
				le.push(left);
				break;
			    }
			}
			
			while(1){
				int pos=right.find('+');
				if(pos!=-1){
				ri.push(right.substr(0,pos));
				right=right.substr(pos+1);
			}
			else{
				ri.push(right);
				break;
			    }
		}
} 

int main(){
	int n=0;
	while(scanf("%d",&n)!=EOF){
		for(int i=1;i<=n;i++){
			string exp;
			cin>>exp;
			int pos=exp.find('=');
			string left=exp.substr(0,pos);
			string right=exp.substr(pos+1);
			get_items(left,right);
			handle_l();
			if(!handle_r(i)) {
			bitems.clear() ;
			continue;
			}
			
			test_finally(i);
		bitems.clear() ;
		cout<<result[i]<<endl;
		}
		
	/*	for(int i=1;i<=n;i++) {
		cout<<result[i]<<endl;
		}*/

	}
	
	return 0;
} 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值