CCF-CSP 201912-3 화학방정식

8220 단어 CCF-CSP 인증

CCF-CSP 201912-3 화학방정식


만점 코드:
#include 
#define FOR(i,s,t) for(int i=(s);i<=(t);i++)
#define ROF(i,s,t) for(int i=(s);i>=(t);i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
#define endl '
' using namespace std; typedef unsigned long long ull; typedef long long ll; const int maxn = 1e3 + 6; const ll mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3f; int readInt(){ int x=0; bool sign=false; char c=getchar(); while(!isdigit(c)){ sign=c=='-'; c=getchar(); } while(isdigit(c)){ x=x*10+c-'0'; c=getchar(); } return sign?-x:x; } ll readLong(){ ll x=0; bool sign=false; char c=getchar(); while(!isdigit(c)){ sign=c=='-'; c=getchar(); } while(isdigit(c)){ x=x*10+c-'0'; c=getchar(); } return sign?-x:x; } string readString(){ string s; char c=getchar(); while(isspace(c)){ c=getchar(); } while(!isspace(c)){ s+=c; c=getchar(); } return s; } int n, e; string equation; string lstr, rstr; vector lfml, rfml; vector lnum, rnum; map lcnt, rcnt; int main() { int T = readInt(); while (T--){ equation = readString(); e = equation.find('='); //cout << e << endl; lstr = equation.substr(0, e); rstr = equation.substr(e + 1); int fst = 0, plus = 0; lfml.clear(); rfml.clear(); lnum.clear(); rnum.clear(); lcnt.clear(); rcnt.clear(); plus = lstr.find('+'); while (plus != -1){ string sub = lstr.substr(0, plus); lfml.push_back(sub); lstr = lstr.substr(plus + 1); plus = lstr.find('+'); } if (lstr != "") lfml.push_back(lstr); //for (auto sub : lfml) cout << sub << " "; plus = rstr.find('+'); while (plus != -1){ string sub = rstr.substr(0, plus); rfml.push_back(sub); rstr = rstr.substr(plus + 1); plus = rstr.find('+'); } if (rstr != "") rfml.push_back(rstr); /*for (auto sub : rfml) cout << sub << " "; cout << endl;*/ for (auto sub : lfml){ int number = 0; for (auto x : sub){ if (x >= '0' && x <= '9'){ number = number * 10 + x - '0'; } else break; } if (number == 0) number = 1; lnum.push_back(number); } for (auto sub : rfml){ int number = 0; for (auto x : sub){ if (x >= '0' && x <= '9'){ number = number * 10 + x - '0'; } else break; } if (number == 0) number = 1; rnum.push_back(number); } for (int kk = 0; kk < lfml.size(); kk++){ int number = lnum[kk]; string sub = lfml[kk]; vector term; int i = 0; for (i = 0; i < sub.size();i++){ if (sub[i] >= '0' && sub[i] <= '9'){continue;} else break; } for (; i < sub.size(); i++){ string element =""; element += sub[i]; if (sub[i] == '(') {term.push_back("(");} else if (sub[i] >= 'A' && sub[i] <= 'Z'){ if (sub[i + 1] >= 'a' && sub[i + 1] <= 'z'){ element += sub[i+1]; i++; } term.push_back(element); } else if (sub[i] == ')'){term.push_back(")");} else if (sub[i] >= '0' && sub[i] <= '9'){ string mm = ""; for (;i < sub.size();i++){ if (sub[i] >= '0' && sub[i] <= '9'){ mm = mm + sub[i]; } else { break; } } i--; term.push_back(mm); } } stack > stk; vector w(term.size()); vector termnum(term.size()); for (int i = 0; i < term.size(); i++){ auto _ = term[i]; if (_[0] >= 'A' && _[0] <= 'Z'){ termnum[i] = 1; } } for (int i = 0; i < term.size(); i++){ auto _ = term[i]; if (_ == "("){ stk.push({"(", i}); } else if (_ == ")"){ w[i] = stk.top().second; stk.pop(); } } for (int i = 0; i < term.size(); i++){ auto _ = term[i]; if (_[0] >= '0' && _[0] <= '9'){ int mm = 0; for (int j = 0; j < _.size(); j++){ mm = mm * 10 + (char)_[j] - '0'; } if (mm == 0) mm = 1; if (term[i-1] == ")"){ for (int j = w[i - 1] + 1; j < i; j++){ if (term[j][0] >= 'A' && term[j][0] <= 'Z') termnum[j] *= mm; } }else{ if (term[i-1][0] >= 'A' && term[i-1][0] <='Z') termnum[i-1] *= mm; } } } for (int i = 0; i < term.size(); i++){ auto _ = term[i]; if (_[0] >= 'A' && _[0] <= 'Z'){ lcnt[_] += number * termnum[i]; } } } for (int kk = 0; kk < rfml.size(); kk++){ int number = rnum[kk]; string sub = rfml[kk]; vector term; int i = 0; for (i = 0; i < sub.size();i++){ if (sub[i] >= '0' && sub[i] <= '9'){continue;} else break; } for (; i < sub.size(); i++){ string element =""; element += sub[i]; if (sub[i] == '(') {term.push_back("(");} else if (sub[i] >= 'A' && sub[i] <= 'Z'){ if (sub[i + 1] >= 'a' && sub[i + 1] <= 'z'){ element += sub[i+1]; i++; } term.push_back(element); } else if (sub[i] == ')'){term.push_back(")");} else if (sub[i] >= '0' && sub[i] <= '9'){ string mm = ""; for (;i < sub.size();i++){ if (sub[i] >= '0' && sub[i] <= '9'){ mm = mm + sub[i]; } else { break; } } i--; term.push_back(mm); } } stack > stk; vector w(term.size()); vector termnum(term.size()); for (int i = 0; i < term.size(); i++){ auto _ = term[i]; if (_[0] >= 'A' && _[0] <= 'Z'){ termnum[i] = 1; } } for (int i = 0; i < term.size(); i++){ auto _ = term[i]; if (_ == "("){ stk.push({"(", i}); } else if (_ == ")"){ w[i] = stk.top().second; stk.pop(); } } for (int i = 0; i < term.size(); i++){ auto _ = term[i]; if (_[0] >= '0' && _[0] <= '9'){ int mm = 0; for (int j = 0; j < _.size(); j++){ mm = mm * 10 + (char)_[j] - '0'; } if (mm == 0) mm = 1; if (term[i-1] == ")"){ for (int j = w[i - 1] + 1; j < i; j++){ if (term[j][0] >= 'A' && term[j][0] <= 'Z') termnum[j] *= mm; } }else{ if (term[i-1][0] >= 'A' && term[i-1][0] <='Z')termnum[i-1] *= mm; } } } for (int i = 0; i < term.size(); i++){ auto _ = term[i]; if (_[0] >= 'A' && _[0] <= 'Z'){ rcnt[_] += number * termnum[i]; } } } int flag = 1; for (auto _ : lcnt){ string s = _.first; int x = rcnt[s]; if (_.second != x){ flag = 0; break; } } for (auto _ : rcnt){ string s = _.first; int x = lcnt[s]; if (_.second != x){ flag = 0; break; } } if (flag == 1) puts("Y"); else puts("N"); } return 0; } /* 11 H2+O2=H2O 2H2+O2=2H2O H2+Cl2=2NaCl H2+Cl2=2HCl CH4+2O2=CO2+2H2O CaCl2+2AgNO3=Ca(NO3)2+2AgCl 3Ba(OH)2+2H2PO4=6H2O+Ba2(PO4)2 3Ba(OH)2+2H2PO4=Ba2(PO4)2+6H2O 4Zn+10HNO3=4Zn(NO3)2+NH4NO3+3H2O 4Na(Au(CN)2)+4NaOH=4Au+8NaCN+2H2O+O2 Cu+As=Cs+Au */

좋은 웹페이지 즐겨찾기