样例全部没问题,但是只有40分,不知道哪里出问题了:
#include <iostream> #include <string> #include <map> #include <sstream> using namespace std; class Fomular { private: string s, sr, sp; map<string,int> reactant; map<string,int> product; map<string,int> rElement; map<string,int> pElement; bool isLowercase(char a) { if (a >= 'a' && a <= 'z') return true; return false; } bool isUppercase(char a) { if (a >= 'A' && a <= 'Z') return true; return false; } bool isDigit(char a) { if (a >= '0' && a <= '9') return true; return false; } void split(string s, decltype(product) &m) { int a{0}, b{0}, i, j, tt; string t; for (i = 0; i < s.length(); i++) { if (s[i] == '+') { b = i; t = s.substr(a, b-a); for (j = 0; j < t.length() && isDigit(t[j]); ++j); stringstream ss; if (j == 0) ss << "1"; else ss << t.substr(0, j); ss >> tt; m[t.substr(j, t.length()-j)] = tt; a = i+1; } } b = i; t = s.substr(a, b-a); for (j = 0; j < t.length() && isDigit(t[j]); ++j); stringstream ss; if (j == 0) ss << "1"; else ss << t.substr(0, j); ss >> tt; m[t.substr(j, t.length()-j)] = tt; } void elemCnt(string fom, decltype(pElement) &pE, int mul = 1) { string t; int level{0}, num; for(int j = 0; j < fom.size(); j++) { if (isUppercase(fom[j]) && isDigit(fom[j+1]) && j+1 < fom.size()) { t = string{fom[j]}; int k = j+1; num = 0; while(isDigit(fom[k]) && k < fom.size()) { num *= 10; num += fom[k] - '0'; k++; } if (num == 0) num = 1; pE[t] += num * mul; j = k - 1; } else if (isUppercase(fom[j]) && isLowercase(fom[j+1]) && j+1 < fom.size()) { t = string{fom[j]}; t.append(string{fom[j+1]}); int k = j+2; num = 0; while(isDigit(fom[k]) && k < fom.size()) { num *= 10; num += fom[k] - '0'; k++; } if (num == 0) num = 1; pE[t] += num * mul; j = k - 1; } else if (isUppercase(fom[j])) { t = string{fom[j]}; pE[t] += mul; } else if (fom[j] == '(') { int numBack{1}, m, k; for (k = j; k < fom.size(); k++) { if (fom[k] == '(') level++; else if (fom[k] == ')') { level--; if (level == 0) m = k; } if (level == 0 && isUppercase(fom[k])) break; } numBack = 0; for (int a = m+1; a < k; a++) { numBack *= 10; numBack += fom[a] - '0'; } if (numBack == 0) numBack = 1; elemCnt(fom.substr(j+1, m-j-1), pE, numBack * mul); j = k; } } } void elemCount(decltype(product) &p, decltype(pElement) &pE) { for(auto i: p) { string fom{i.first}; elemCnt(fom, pE, i.second); } } public: Fomular(string s_) { int eq; s = s_; eq = s.find('='); sr = s.substr(0,eq); sp = s.substr(eq+1,s.length()-eq-1); split(sr, reactant); split(sp, product); elemCount(reactant, rElement); elemCount(product, pElement); //----print------ // for (auto i: rElement) // { // cout << i.first << "#" << i.second << " "; // } // cout << "= "; // for (auto i: pElement) // { // cout << i.first << "#" << i.second << " "; // } // cout << endl; // ----print------ } char getAnswer() { if (rElement.size() != pElement.size()) return 'N'; for (auto i:rElement) { if(i.second != pElement[i.first]) return 'N'; } return 'Y'; } }; int main() { int n; cin >> n; string s; getline(cin, s); for (int i = 0; i < n; ++i) { getline(cin, s); Fomular f{s}; cout << f.getAnswer() << endl; } return 0; }