Chemical equation

Title description

Class 102 writes chemical equations every day. Now we have to find ways to correct the equations. The order of the reactants in the equation can be different, with spaces in between. There will be no precipitation and gas signs and reaction conditions. There are wrong coefficients (when the subscript is wrong, if CO2 is CO3, the coefficient is also wrong. The chemical formulas with the same element and different coefficients will not appear in the reactant or product, such as: CO and CO2 will not appear in the reactant or product Middle), wrong chemical formula, lack of reactants, elements on both sides are not conserved.

Input

Enter N, M in the first row

Enter a string of length N in the second row as the correct equation

Enter a string of length M in the third row for the equation to be corrected

Output

Correct output RIGHT

Errors WRONG is output in the first row, and the cause of the error is output in the second row (only the coefficient is incorrect is 1, other errors and more than two errors are 2)

Sample input

34  29
2Na  O H + H2S O4 = Na 2 S O4+2H2O
H2S O4 +2NaO H =Na2 S O4+2H2O

Sample output

RIGHT

prompt

34  25

2Na OH + H2S O4 = Na 2 SO4 + 2H2O

H2S O4
+NaO H=2H2O +2 K Cl

WRONG

2

1<=N<=100

1<=M<=100

Code

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define rep(i, j, k) for (register int i = (j); i <= (k); i ++)
using namespace std;
char a[1005],b[1005];
vector< string >ys1,ys2;
int fff,h1,h2;
template <class T> inline void read(T &x) {
    x = 0;
    register char c = getchar();
    register bool f = 0;
    while (!isdigit(c)) f ^= c == '-', c = getchar();
    while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
    if (f) x = -x;
}
string getnum(string s)
{
    string tmp="";
    int len=s.length();
    for(int i=0;i<len;i++)
    {
        if(s[i]<='9'&&s[i]>='0')
        {
            tmp=tmp+s[i];
        }
    }
    return tmp;
}
string getchar(string s)
{
    string tmp="";
    int len=s.length();
    for(int i=0;i<len;i++)
    {
        if(s[i]>'9'||s[i]<'0')
        {
            tmp=tmp+s[i];
        }
    }
    return tmp;
}
bool check(char x)
{
    if(x<='9'&&x>='0') return true;
    if(x<='Z'&&x>='A') return true;
    if(x<='z'&&x>='a') return true;
    if(x==' '||x=='+'||x=='=') return true;
    return false;
}
int main()
{
    string tmp=""; 
    gets(a);
    gets(a);
    gets(b);
    int n=strlen(a);
    int m=strlen(b);
    for(int i=0;i<n;i++)
    {
        if(fff==0&&a[i]!=' ') h1+=a[i];
        if(a[i]=='=') fff=1;
        if(check(a[i])!=1) continue;
        if(a[i]=='+'||a[i]=='=')
        {
            ys1.push_back(tmp);
            tmp="";
        }
        else if(a[i]!=' ')
        {
            tmp=tmp+a[i];
        }
    }
    if(tmp!="")
    {
        ys1.push_back(tmp);
    }
    tmp="";
    fff=0;
    for(int i=0;i<m;i++)
    {
        if(fff==0&&b[i]!=' ') h2+=b[i];
        if(b[i]=='=') fff=1;
        if(check(b[i])!=1) continue;
        if(b[i]=='+'||b[i]=='=')
        {
            ys2.push_back(tmp);
            tmp="";
        }
        else if(b[i]!=' ')
        {
            tmp=tmp+b[i];
        }
    }
    if(tmp!="")
    {
        ys2.push_back(tmp);
        tmp="";
    }
    sort(ys1.begin(),ys1.end());
    sort(ys2.begin(),ys2.end());
    if(ys1==ys2)
    {
        if(h1==h2)
        {
            cout<<"RIGHT"<<endl;
            return 0;
        }
    }
    int c1=0,c2=0;
    if(ys1.size()!=ys2.size())
        c1=1,c2=1;
    else
    {
        n=ys1.size();
        for(int i=0;i<n;i++)
        {
            if(ys1[i]!=ys2[i])
            {
                if(getnum(ys1[i])!=getnum(ys2[i]))
                    c1=1;
                if(getchar(ys1[i])!=getchar(ys2[i]))
                    c2=1;
            }
        }
    }
    cout<<"WRONG"<<endl;
    if(c1==1&&c2==0) cout<<1<<endl;
    else cout<<2<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/LJA001162/p/12678601.html