2019年12月ccf csp認證第三題-判斷是否配平化學方程式

#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分,不少地方沒開函數,直接複製了代碼,因此代碼有點繁雜,有時間再修改一下。c++

相關文章
相關標籤/搜索