模拟赛20181015 模拟

一道模拟的题目,给出化学方程式判断元素数量是否相同

开始写了一堆循环异常麻烦,然后发现...

递归真香

有括号的情况直接去掉计算完系数之后递归统计答案不就完了,在最底层处统计答案,递归时将系数带上不就解决了多层吗,咕咕咕我太蠢了

最后利用map统计答案,要多用map啊,map多香,用hash全是异端(我本来就用的是hash....)

大模拟本来觉得很难,后来发现这种题需要合理的编排结构,并且提前将各种情况分好,需要先分析题目再写(不就本来就是这样么)

#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i=x;i<=y;i++)
using namespace std;

typedef map<string,int>::iterator it;
map<string,int> mp,bf;
string s,t;int n;

void make(string s,int cnt,int ty){ int now=0;
    while(now<s.size()){
        if(s[now]=='('){
            int tmp2=now,tot=1;
            while(tot){
                if(s[++tmp2]=='(') tot++;
                else if(s[tmp2]==')') tot--;}
            
            int tmp3=tmp2+1,num=0;
            while(tmp3<s.size()&&isdigit(s[tmp3]))
                num=num*10+s[tmp3]-'0',tmp3++;
            num=max(num,1);
            make(s.substr(now+1,tmp2-now-1),num*cnt,ty);
            now=tmp3;
        }else{
            int tmp4=now,num=0;
            while(islower(s[++tmp4]));
            string sx=s.substr(now,tmp4-now);now=tmp4;
            while(isdigit(s[now])) num=num*10+s[now]-'0',now++;
            mp[sx]+=max(num,1)*cnt*ty;
        }
    }    
}
/*
    统计答案的步骤
    1.将串的长度确定的找出来
    将后缀的数字/前缀的数字确认的算出来
    保证答案不为0
    进行递归统计 
*/
int main(){
    freopen("chem.in","r",stdin);
    freopen("chem.out","w",stdout);
    
    cin>>s;int now=0;
    while(now<s.size()){
        int tmp=now,cnt=0;
        while(++tmp<s.size()&&s[tmp]!='+');//++顺序不同
        while(isdigit(s[now])) cnt=cnt*10+s[now]-'0',now++;//前缀数字 
        cnt=max(cnt,1);//保持答案不为0 
        make(s.substr(now,tmp-now),cnt,1);now=tmp+1;
    }    
    bf=mp;t=s;cin>>n;
    rep(i,1,n){ mp=bf;bool FG=1;
        cin>>s;int now=0;
        
        while(now<s.size()){
            int tmp=now,cnt=0;
            while(++tmp<s.size()&&s[tmp]!='+');
            while(isdigit(s[now])) cnt=cnt*10+s[now]-'0',now++;
            cnt=max(cnt,1);
            make(s.substr(now,tmp-now),cnt,-1);now=tmp+1;
        }
            
        for(it i=mp.begin();i!=mp.end();i++)
        if(i->second!=0){cout<<t<<"!="<<s<<endl;FG=0;break;} 
        
        if(FG) cout<<t<<"=="<<s<<endl;} 
    return 0;} 

完结撒花

样例输入

C2H5OH+3O2+3(SiO2)
7
2CO2+3H2O+3SiO2
2C+6H+13O+3Si
99C2H5OH+3SiO2
3SiO4+C2H5OH
C2H5OH+3O2+3(SiO2)+Ge
3(Si(O)2)+2CO+3H2O+O2
2CO+3H2O+3O2+3Si

样例输出

C2H5OH+3O2+3(SiO2)==2CO2+3H2O+3SiO2
C2H5OH+3O2+3(SiO2)==2C+6H+13O+3Si
C2H5OH+3O2+3(SiO2)!=99C2H5OH+3SiO2
C2H5OH+3O2+3(SiO2)==3SiO4+C2H5OH
C2H5OH+3O2+3(SiO2)!=C2H5OH+3O2+3(SiO2)+Ge
C2H5OH+3O2+3(SiO2)==3(Si(O)2)+2CO+3H2O+O2
C2H5OH+3O2+3(SiO2)!=2CO+3H2O+3O2+3Si

样例2输入

ABCDEFGH+IJKLMONPQ+RSTUVWXYZ
7
A+B+C+D+E+F+G+H+I+K+L+M+O+N+P+Q+R+S+T+U+V+W+X+Y+Z
L+M+O+N+P+Q+R+S+T+U+V+W+X+Y+Z+A+B+C+D+E+F+G+H+I+J+K
A+B+C+D+E+F+G+H+I+J+K+L+M+O+N+P+Q+R+S+T+U+V+W+X+Y+Z
ABC+MONPQRS+TUVWXYZD+EFGHIJKL
((B(E(F)(G)(H)(I)(J)K(((((((((((((((((LM((O))N)))))))))))))))))PQ(R)S(T)U)VWX)YZ)((((A))))+(((CD)))
((AB(E(F)(G)(H)(I)(J)K(((((((((((((((((LM((O))N)))))))))))))))))PQ(R)S(T)U)VWX)YZ)((((A))))+(((CD)))
((B(E(F)(G)(H)(I)(J)K(((((((((((((((((LM((O))N)))))))))))))))))PQ(R)S(T)U)VWX)YZ)((((Ai))))+(((CD)))

样例2输出

ABCDEFGH+IJKLMONPQ+RSTUVWXYZ!=A+B+C+D+E+F+G+H+I+K+L+M+O+N+P+Q+R+S+T+U+V+W+X+Y+Z
ABCDEFGH+IJKLMONPQ+RSTUVWXYZ==L+M+O+N+P+Q+R+S+T+U+V+W+X+Y+Z+A+B+C+D+E+F+G+H+I+J+K
ABCDEFGH+IJKLMONPQ+RSTUVWXYZ==A+B+C+D+E+F+G+H+I+J+K+L+M+O+N+P+Q+R+S+T+U+V+W+X+Y+Z
ABCDEFGH+IJKLMONPQ+RSTUVWXYZ==ABC+MONPQRS+TUVWXYZD+EFGHIJKL
ABCDEFGH+IJKLMONPQ+RSTUVWXYZ==((B(E(F)(G)(H)(I)(J)K(((((((((((((((((LM((O))N)))))))))))))))))PQ(R)S(T)U)VWX)YZ)((((A))))+(((CD)))
ABCDEFGH+IJKLMONPQ+RSTUVWXYZ!=((AB(E(F)(G)(H)(I)(J)K(((((((((((((((((LM((O))N)))))))))))))))))PQ(R)S(T)U)VWX)YZ)((((A))))+(((CD)))
ABCDEFGH+IJKLMONPQ+RSTUVWXYZ!=((B(E(F)(G)(H)(I)(J)K(((((((((((((((((LM((O))N)))))))))))))))))PQ(R)S(T)U)VWX)YZ)((((Ai))))+(((CD)))

 

posted @ 2018-10-18 18:22  ASDIC减除  阅读(120)  评论(0编辑  收藏  举报