CCF-CSP 201912-3 化学方程式

#include 

using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

void split(string s, vector &v, string c)//分割字符串
{
    size_t pos1, pos2;
    pos2 = s.find(c);
    pos1 = 0;
    while (string::npos != pos2)
    {
        v.push_back(s.substr(pos1, pos2 - pos1));
        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if (pos1 != s.length())
        v.push_back(s.substr(pos1));
}

void solve2(string s, map &m)//处理以加号为分割的块
{
    stringstream ss;
    ss << s[0];
    for (int i = 1; i < s.length(); i++)
    {
        if (isdigit(s[i]) && !isdigit(s[i - 1]))
            ss << ' ';
        else if (isupper(s[i]))
            ss << ' ';
        else if (s[i] == '(' || s[i] == ')')
            ss << " ";
        ss << s[i];
    }
    string temp;
    vector v;
    vector> element;
    vector leftpar;
    while (ss >> temp)
        v.push_back(temp);
    int times = 1;
    if (isdigit(v[0][0]))
        times = stoi(v[0]);
    for (int i = isdigit(v[0][0]) ? 1 : 0; i < v.size(); i++)
    {
        if (isupper(v[i][0]))
        {
            if (i + 1 < v.size() && isdigit(v[i + 1][0]))
            {
                int t = stoi(v[i + 1]);
                element.push_back({v[i], t});
                i++;
            }
            else
            {
                element.push_back({v[i], 1});
            }
        }
        else if (v[i] == "(")
        {
            leftpar.push_back(element.size());
        }
        else if (v[i] == ")")
        {
            if (i + 1 < v.size() && isdigit(v[i + 1][0]))
            {
                int st = leftpar.back();
                int t = stoi(v[i + 1]);
                leftpar.pop_back();
                for (int j = st; j < element.size(); j++)
                {
                    element[j].second *= t;
                }
                i++;
            }
        }
    }
    for (auto &x:element)
    {
        m[x.first] += x.second * times;
    }
}

void solve(string s, map &m)//处理左边或右边
{
    vector v;
    split(s, v, "+");
    for (int i = 0; i < v.size(); i++)
        solve2(v[i], m);
}

bool equal(map &m1, map &m2)
{
    if (m1.size() != m2.size())
        return false;
    for (auto &x:m1)
    {
        if (m2[x.first] != x.second)
            return false;
    }
    return true;
}

int main()
{
    IOS;
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        string str;
        cin >> str;
        map m1, m2;
        vector part;
        split(str, part, "=");
        solve(part[0], m1);
        solve(part[1], m2);
//        cout<<"m1 :: ";
//        for (auto &x:m1)
//        {
//            cout << x.first << "[" << x.second << "] ";
//        }
//        cout << endl;
//        cout<<"m1 :: ";
//        for (auto &x:m2)
//        {
//            cout << x.first << "[" << x.second << "] ";
//        }
//        cout << endl;
        if (equal(m1, m2))
            cout << "Y" << endl;
        else
            cout << "N" << endl;
    }
    return 0;
}

 

你可能感兴趣的:(CCF-CSP)