码迷,mamicode.com
首页 > 其他好文 > 详细

2019年12月ccf csp认证第三题-判断是否配平化学方程式

时间:2019-12-18 12:43:40      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:认证   top   str   its   std   main   ccf   bre   很多   

#include<bits/stdc++.h>
using namespace std;
int n;
string s;
void solve_son(queue<string>&q,int bei,map<string,int>&m)
{
    while(!q.empty())
    {
        string ts = q.front();
        q.pop();
        if(ts=="(")
        {
            int left = 1,right = 0;
            queue<string>tq;
            while(left!=right)
            {
                if(q.front()=="(")
                    left++;
                if(q.front()==")")
                    right++;
                if(left==right)
                    break;
                tq.push(q.front());
                q.pop();
            }
            q.pop();
            stringstream s1;
            string s2 = q.front();
            s1<<s2;
            int beit;
            s1>>beit;
            q.pop();
            solve_son(tq,bei*beit,m);
        }
        else
        {
            stringstream s1;
            string s2 = q.front();
            s1<<s2;
            int beit;
            s1>>beit;
            q.pop();
            m[ts]+= bei*beit;
        }
    }
}
void solve(stack<string>&st,map<string,int>&m)
{
    stack<string>rt;
    while(!st.empty())
    {
        rt.push(st.top());
        st.pop();
    } 
    queue<string>q;
    stringstream s1;
    string s2 = rt.top();
    s1<<s2;
    int bei;
    s1>>bei;
    rt.pop();
    while(rt.size())
    {
        q.push(rt.top());
        rt.pop();
    }
    solve_son(q,bei,m);
}
bool check()
{
    int i;
    map<string,int>mleft;
    map<string,int>mright;
    stack<string>st;
    for(i = 0;s[i]!==;i++)
    {
        if(i==0)
        {
            if(s[i]<=9&&s[i]>=0)
            {
                string t;
                while(s[i]<=9&&s[i]>=0)
                {
                    t+=s[i];
                    i++;
                }
                st.push(t);
            }
            else
                st.push("1");
        }    
        if(s[i]==+||s[i+1]===)
        {
            if(s[i]<=Z&&s[i]>=A)
            {
                string t;
                t+=s[i];
                if((i+1)<s.length()&&s[i+1]<=z&&s[i+1]>=a)
                {
                    t+=s[i+1];
                    i++;
                }    
                st.push(t);
                if(!((i+1)<s.length()&&s[i+1]<=9&&s[i+1]>=0))
                    st.push("1");
            }        
            else if(s[i]<=9&&s[i]>=0)
            {
                string t;
                t+=s[i];
                while((i+1)<s.length()&&s[i+1]<=9&&s[i+1]>=0)
                {
                    t+=s[i+1];
                    i++;
                }
                st.push(t);
            }
            else if(s[i]==()
                st.push("(");
            else if(s[i]==))
            {
                st.push(")");
                if(!((i+1)<s.length()&&s[i+1]<=9&&s[i+1]>=0))
                    st.push("1");
            }    
            solve(st,mleft);
            if(s[i+1]!==)
            {
                if(s[i+1]<=9&&s[i+1]>=0)
                {
                    string t;
                    while(s[i+1]<=9&&s[i+1]>=0)
                    {
                        t+=s[i+1];
                        i++;
                    }
                    st.push(t);
                }
                else
                    st.push("1");
            }
        }    
        else if(s[i]<=Z&&s[i]>=A)
        {
            string t;
            t+=s[i];
            if((i+1)<s.length()&&s[i+1]<=z&&s[i+1]>=a)
            {
                t+=s[i+1];
                i++;
            }    
            st.push(t);
            if(!((i+1)<s.length()&&s[i+1]<=9&&s[i+1]>=0))
                st.push("1");
        }        
        else if(s[i]<=9&&s[i]>=0)
        {
            string t;
            t+=s[i];
            while((i+1)<s.length()&&s[i+1]<=9&&s[i+1]>=0)
            {
                t+=s[i+1];
                i++;
            }
            st.push(t);
        }
        else if(s[i]==()
            st.push("(");
        else if(s[i]==))
        {
            st.push(")");
            if(!((i+1)<s.length()&&s[i+1]<=9&&s[i+1]>=0))
                st.push("1");
        }    
    }
    if(st.size())
        solve(st,mleft);
    /*---------------------------------------------------*/
    for(;i<s.length();i++)
    {
        if(s[i]===)
        {
            i++;
            if(s[i]<=9&&s[i]>=0)
            {
                string t;
                while(s[i]<=9&&s[i]>=0)
                {
                    t+=s[i];
                    i++;
                }
                st.push(t);
            }
            else
                st.push("1");
        }
        if(s[i]==+||(i+1)==s.length())
        {
            if(s[i]<=Z&&s[i]>=A)
            {
                string t;
                t+=s[i];
                if((i+1)<s.length()&&s[i+1]<=z&&s[i+1]>=a)
                {
                    t+=s[i+1];
                    i++;
                }    
                st.push(t);
                if(!((i+1)<s.length()&&s[i+1]<=9&&s[i+1]>=0))
                    st.push("1");
            }        
            else if(s[i]<=9&&s[i]>=0)
            {
                string t;
                t+=s[i];
                while((i+1)<s.length()&&s[i+1]<=9&&s[i+1]>=0)
                {
                    t+=s[i+1];
                    i++;
                }
                st.push(t);
            }
            else if(s[i]==()
                st.push("(");
            else if(s[i]==))
            {
                st.push(")");
                if(!((i+1)<s.length()&&s[i+1]<=9&&s[i+1]>=0))
                    st.push("1");
            }    
            solve(st,mright);
            if((i+1)!=s.length())
            {
                if(s[i+1]<=9&&s[i+1]>=0)
                {
                    string t;
                    while(s[i+1]<=9&&s[i+1]>=0)
                    {
                        t+=s[i+1];
                        i++;
                    }
                    st.push(t);
                }
                else
                    st.push("1");
            }
        }    
        else if(s[i]<=Z&&s[i]>=A)
        {
            string t;
            t+=s[i];
            if((i+1)<s.length()&&s[i+1]<=z&&s[i+1]>=a)
            {
                t+=s[i+1];
                i++;
            }    
            st.push(t);
            if(!((i+1)<s.length()&&s[i+1]<=9&&s[i+1]>=0))
                st.push("1");
        }        
        else if(s[i]<=9&&s[i]>=0)
        {
            string t;
            t+=s[i];
            while((i+1)<s.length()&&s[i+1]<=9&&s[i+1]>=0)
            {
                t+=s[i+1];
                i++;
            }
            st.push(t);
        }
        else if(s[i]==()
            st.push("(");
        else if(s[i]==))
        {
            st.push(")");
            if(!((i+1)<s.length()&&s[i+1]<=9&&s[i+1]>=0))
                st.push("1");
        }    
    }
    if(st.size())
        solve(st,mright);
        
    map<string,int>::iterator leftiter;
    map<string,int>::iterator rightiter;
    leftiter = mleft.begin();
    rightiter = mright.begin();
    while(leftiter!=mleft.end()&&rightiter!=mright.end())
    {
        if(leftiter->first!=rightiter->first||leftiter->second!=rightiter->second)
            return false;
        leftiter++;
        rightiter++;
    }
    if(leftiter==mleft.end()&&rightiter==mright.end())
        return true;
    return false;
}
int main()
{
    cin>>n;
    while(n--)
    {
        cin>>s;
        if(check())
            cout<<Y<<endl;
        else
            cout<<N<<endl;
    }
    return 0;
}

这题写了1个多小时,所幸最后这题100分,很多地方没开函数,直接复制了代码,所以代码有点繁杂,有时间再修改一下。

2019年12月ccf csp认证第三题-判断是否配平化学方程式

标签:认证   top   str   its   std   main   ccf   bre   很多   

原文地址:https://www.cnblogs.com/loganacmer/p/12059097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!