费了九牛二虎之力照着80分写法却只拿了70分,还没来得及写递归
先占坑吧 等写满分了再更
#include<bits/stdc++.h>
using namespace std;
map<string,int> m1,m2;
vector<string> v;
string str;
int find(int i)
{
int cnt3=0;
while(1)
{
if(str[i]==')') break;
i++;
}
i++;
while(str[i]>='0'&&str[i]<='9')//取系数
{
cnt3*=10;
cnt3+=str[i]-'0';
i++;
}
return cnt3;
}
int main()
{
int t;cin>>t;
while(t--)
{
cin>>str;
m1.clear();m2.clear();
v.clear();
int i=0;
int cnt1=0,cnt2=0;
int k=str.find('=');
while(i<k)
{
//cout<<i<<endl;
if(str[i-1]=='+'||i==0)//到系数之前初始化
{
cnt1=0;
}
while(str[i]>='0'&&str[i]<='9')//取系数
{
cnt1*=10;
cnt1+=str[i]-'0';
i++;
}
int cnt3=0;
while(1)//取每个元素及其下标
{
cnt2=0;
string s;
if(str[i]=='(')//取括号外数字
{
cnt3=find(i);
//cout<<"jump to "<<jmp<<endl;
//cout<<cnt3<<endl;
i++;
}
if(str[i]>='A'&&str[i]<='Z')//取元素
{
s+=str[i];
if(str[i+1]>='a'&&str[i+1]<='z')
{
s+=str[i+1];
i++;
}
}
i++;
while(str[i]>='0'&&str[i]<='9')
{
cnt2*=10;
cnt2+=str[i]-'0';
i++;
}
if(cnt1==0&&cnt2==0&&cnt3==0) m1[s]+=1;
else if(cnt1==0&&cnt2==0) m1[s]+=cnt3;
else if(cnt2==0&&cnt3==0) m1[s]+=cnt1;
else if(cnt1==0&&cnt3==0) m1[s]+=cnt2;
else if(cnt1==0) m1[s]+=cnt2*cnt3;
else if(cnt2==0) m1[s]+=cnt1*cnt3;
else if(cnt3==0) m1[s]+=cnt1*cnt2;
else m1[s]+=cnt1*cnt2*cnt3;
//cout<<i<<' '<<s<<endl;
//cout<<s<<' '<<cnt1<<' '<<cnt2<<' '<<cnt3<<endl;
if(s.length()>0)
v.push_back(s);
if(str[i]=='+'||str[i]=='=') break;
}
i++;
}
//cout<<i<<endl;
while(i<str.length())
{
//cout<<i<<endl;
if(str[i-1]=='+'||str[i-1]=='=')//到系数之前初始化
{
cnt1=0;
}
while(str[i]>='0'&&str[i]<='9')//取系数
{
cnt1*=10;
cnt1+=str[i]-'0';
i++;
}
int cnt3=0,jmp;
while(1)//取每个元素及其下标
{
cnt2=0;
string s;
if(str[i]=='(')//取括号外数字
{
cnt3=find(i);
i++;
}
if(str[i]>='A'&&str[i]<='Z')//取元素
{
s+=str[i];
if(str[i+1]>='a'&&str[i+1]<='z')
{
s+=str[i+1];
i++;
}
}
i++;
while(str[i]>='0'&&str[i]<='9')
{
cnt2*=10;
cnt2+=str[i]-'0';
i++;
}
if(cnt1==0&&cnt2==0&&cnt3==0) m2[s]+=1;
else if(cnt1==0&&cnt2==0) m2[s]+=cnt3;
else if(cnt2==0&&cnt3==0) m2[s]+=cnt1;
else if(cnt1==0&&cnt3==0) m2[s]+=cnt2;
else if(cnt1==0) m2[s]+=cnt2*cnt3;
else if(cnt2==0) m2[s]+=cnt1*cnt3;
else if(cnt3==0) m2[s]+=cnt1*cnt2;
else m2[s]+=cnt1*cnt2*cnt3;
//cout<<i<<' '<<s<<endl;
//cout<<s<<' '<<s.length()<<' '<<cnt1<<' '<<cnt2<<' '<<cnt3<<endl;
if(s.length()>0)
v.push_back(s);
if(str[i]=='+'||i>=str.length()) break;
}
i++;
}
bool flag=0;
for(int i=0;i<v.size();i++)
{
string s=v[i];
//cout<<s<<' '<<m1[s]<<' '<<m2[s]<<endl;
if(m1[s]!=m2[s])
{
flag=1;
cout<<"N"<<endl;
break;
}
}
if(!flag) cout<<"Y"<<endl;
}
return 0;
}
/*
1
(NH4)(Cl)+(NH4)(Cl)=2(NH4)(Cl)
*/