CCF-201912-3
作者:互联网
CCF日常学习更新
由于我也是在学习,所以代码可能有些繁冗,希望能一起学习
AC代码
#include<bits/stdc++.h>
using namespace std;
int Str2N(string S)
{
int i,sum=0;
for(i=0;i<S.length();i++)
{
sum=10*sum+S[i]-'0';
}
return sum;
}
void Get(string element,map<string,int> &M,int K)
{
int idn,idx,idy,idz,K0,K1,count;
string ele,s;
map<string,int>::iterator iter;
//处理括号
idn=0;
while(element.find("(",idn)!=element.npos)
{
//括号
idn=element.find("(",idn);
//另一个括号
for(idx=idn,count=0;idx<element.length();idx++)
{
if(element[idx]=='(')count++;
if(element[idx]==')')count--;
if(count==0)break;
}
//括号后面
idy=idx+1;
if(idy<element.length())
while(element[idy]>='0'&&element[idy]<='9')idy++;
if(idy>idx+1)
{
s=element.substr(idx+1,idy-idx-1);
K0=Str2N(s);
}
else K0=1;
s=element.substr(idn+1,idx-idn-1);
Get(s,M,K*K0);
element.erase(idn,idy-idn);
}
//处理完括号
idn=-1;
while(element.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ",idn+1)!=element.npos)
{
//无括号元素
idx=element.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ",idn+1);
idy=idx+1;
while(element[idy]>='a'&&element[idy]<='z')idy++;
ele=element.substr(idx,idy-idx);
//后置系数K1
idz=idy;
if(idz<element.length())
while(element[idz]>='0'&&element[idz]<='9')idz++;
if(idz>idy)
{
s=element.substr(idy,idz-idy);
K1=Str2N(s);
}
else K1=1;
//存入元素
if(M.find(ele)!=M.end())
{
iter=M.find(ele);
iter->second=iter->second+K*K1;
}
else
{
M.insert({ele,K*K1});
}
idn=idx;
}
}
int main()
{
int i,idn,idx,idy,flag,K,N;
string str,element,s,s1,s2;
map<string,int> left,right;
map<string,int>::iterator iter,it;
cin>>N;
for(i=0;i<N;i++)
{
cin>>str;
idn=str.find("=");
s1=str.substr(0,idn);
s2=str.substr(idn+1,str.length()-idn-1);
s1=s1+"+";
s2=s2+"+";
idn=-1;
while(s1.find("+",idn+1)!=s1.npos)
{
idx=s1.find_first_of("+",idn+1);
element=s1.substr(idn+1,idx-idn-1);
//提取系数K
idn=element.find_first_of("0123456789");
if(idn==0)
{
idy=element.find_first_not_of("0123456789");
s=element.substr(idn,idy-idn);
K=Str2N(s);
element.erase(0,idy);
}
else K=1;
Get(element,left,K);
idn=idx;
}
idn=-1;
while(s2.find_first_of("+",idn+1)!=s2.npos)
{
idx=s2.find_first_of("+",idn+1);
element=s2.substr(idn+1,idx-idn-1);
//提取系数
idn=element.find_first_of("0123456789");
if(idn==0)
{
idy=element.find_first_not_of("0123456789");
s=element.substr(idn,idy-idn);
K=Str2N(s);
element.erase(0,idy);
}
else K=1;
Get(element,right,K);
idn=idx;
}
for(iter=left.begin(),flag=1;iter!=left.end();iter++)
{
it=right.find(iter->first);
if(it==right.end()||iter->second!=it->second)flag=0;
}
if(left.size()!=right.size())flag=0;
left.clear();
right.clear();
if(flag==0)cout<<"N"<<endl;
else cout<<"Y"<<endl;
}
return 0;
}
标签:idx,idy,iter,element,idn,201912,CCF,find 来源: https://blog.csdn.net/Whilst_learner/article/details/113817159