Day9 化学方程式配平
Posted 未来可期-2018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Day9 化学方程式配平相关的知识,希望对你有一定的参考价值。
该题的难点在于括号的嵌套,如Na6((OH)(OH)2)3,比如OH在跳出嵌套之后需乘以紧接着其外的嵌套下标,我的解法是先一遍处理以下找到每个左括号和与之配置的右括号的位置,然后使用递归,每遇到左括号递归,遇到右括号返回map。
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
//保存左括号和与之匹配的右括号的下标
map<int,int> bracket;
//2H2+O2----> H2:2,O2:1
void deal_expr(string&lexpr,vector<pair<string,int> >&lformula)
int i=0;
for(size_t pos=lexpr.find("+");pos!=string::npos;pos=lexpr.find("+",pos+1))
string tmp=lexpr.substr(i,pos-i);
i=pos+1;
int j=0;
while(j<tmp.size()&&isdigit(tmp[j]))
j++;
string coef=tmp.substr(0,j);
if(coef.empty())
lformula.push_back(make_pair(tmp.substr(j),1));
else
lformula.push_back(make_pair(tmp.substr(j),stoi(tmp.substr(0,j))));
string tmp=lexpr.substr(lexpr.find_last_of("+")+1);
int j=0;
while(j<tmp.size()&&isdigit(tmp[j]))
j++;
string coef=tmp.substr(0,j);
if(coef.empty())
lformula.push_back(make_pair(tmp.substr(j),1));
else
lformula.push_back(make_pair(tmp.substr(j),stoi(tmp.substr(0,j))));
//Na((OH)(OH)) -> Na:1,O:2,H:2
//term为化学式,i为处理到该字符串的位置
map<string,int> deal_term(string term, int i)
map<string,int>Map;
if(term.empty())
return Map;
while(i<term.size())
if(term[i]=='(')//有左括号,递归处理括号内部
int j=bracket[i];
map<string,int>tmp=deal_term(term,i+1);
int cnt=0;
j+=1;
while(j<term.size()&&isdigit(term[j]))
j++;
cnt++;
int num=1;
if(term.substr(bracket[i]+1,cnt).empty())
num=1;
else
num=stoi(term.substr(bracket[i]+1,cnt));
for(auto it=tmp.begin();it!=tmp.end();++it)
Map[it->first]+=it->second*num;
i=j;
else if(term[i]==')')//遇到右括号返回
return Map;
else if(isupper(term[i])&&i+1<term.size()&&islower(term[i+1]))//Fe10
int j=i+2;
while(j<term.size()&&isdigit(term[j]))
++j;
if(term.substr(i+2,j-i-2).empty())
Map[term.substr(i,2)]+=1;
else
Map[term.substr(i,2)]+=stoi(term.substr(i+2,j-i-2));
i=j;
else if(isupper(term[i])&&i+1<term.size()&&isupper(term[i+1]))//OH
Map[term.substr(i,1)]+=1;
i+=1;
else if(isupper(term[i])&&i+1<term.size()&&isdigit(term[i+1]))//H2
int j=i+1;
while(j<term.size()&&isdigit(term[j]))
j++;
if(term.substr(i+1,j-i-1).empty())
Map[term.substr(i,1)]+=1;
else
Map[term.substr(i,1)]+=stoi(term.substr(i+1,j-i-1));
i=j;
else if(isupper(term[i])&&(i+1>=term.size()||term[i+1]=='('||term[i+1]==')'))//O||HO)||(OH)
Map[term.substr(i,1)]+=1;
i++;
return Map;
//H2:2->H:4
void deal_formula(vector<pair<string,int> >&formula,map<string,int>&Map)
for(auto it=formula.begin();it!=formula.end();++it)
int num=it->second;
string term=it->first;
term="("+term+")"+to_string(num);
//匹配左右括号
stack<int>S;
bracket.clear();
for(int i=0;i<term.length();++i)
if(term[i]=='(')
S.push(i);
else if(term[i]==')')
bracket[S.top()]=i;
S.pop();
map<string,int>tmp=deal_term(term,0);
for(auto iter=tmp.begin();iter!=tmp.end();++iter)
Map[iter->first]+=iter->second;
int main()
int n;
cin>>n;
//表达式,左部分,右部分
string equation,lexpr,rexpr;
//存储左右化学元素的个数
map<string,int>left,right;
while(n--)
left.clear();
right.clear();
cin>>equation;
//2H2+O2=2H2O--->[2H2,O2] [2H2O]
lexpr=equation.substr(0,equation.find("="));
rexpr=equation.substr(equation.find("=")+1);
vector<pair<string,int> >lformula,rformula;
deal_expr(lexpr,lformula);
deal_expr(rexpr,rformula);
deal_formula(lformula,left);
deal_formula(rformula,right);
if(left==right)
cout<<"Y"<<endl;
else
cout<<"N"<<endl;
return 0;
/**
11
H2+O2=H2O
2H2+O2=2H2O
H2+Cl2=2NaCl
H2+Cl2=2HCl
CH4+2O2=CO2+2H2O
CaCl2+2AgNO3=Ca(NO3)2+2AgCl
3Ba(OH)2+2H3PO4=6H2O+Ba3(PO4)2
3Ba(OH)2+2H3PO4=Ba3(PO4)2+6H2O
4Zn+10HNO3=4Zn(NO3)2+NH4NO3+3H2O
4Au+8NaCN+2H2O+O2=4Na(Au(CN)2)+4NaOH
Cu+As=Cs+Au
**/
/**
Na+H2O+O2H=Na(OH(OH)(OH))
**/
以上是关于Day9 化学方程式配平的主要内容,如果未能解决你的问题,请参考以下文章