#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分,很多地方没开函数,直接复制了代码,所以代码有点繁杂,有时间再修改一下。
原文地址:https://www.cnblogs.com/loganacmer/p/12059097.html
时间: 2024-10-31 15:33:28