201912-3 化学方程式
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void analyse(string s, vector<string>& left, vector<string>& right) {
int l = s.length();
int start = 0, end = 0, flag = 0;
while (end <= l) {
if (s[end] == '+') {
if (flag == 0) {
left.push_back(s.substr(start, end - start));
start = end + 1;
}
else if (flag == 1) {
right.push_back(s.substr(start, end - start));
start = end + 1;
}
}
else if (s[end] == '=') {
left.push_back(s.substr(start, end - start));
start = end + 1;
flag = 1;
}
else if (end == l) {
right.push_back(s.substr(start, end - start));
}
end++;
}
}
void solve(string s, vector<pair<string, int> >& va) {
map<string, int> m;
stack<pair <string, int> > st;
int coef = 1;
int start = 0, end = 0, l = s.length();
if (s[0] >= '0' && s[0] <= '9') {
while (s[end] >= '0' && s[end] <= '9') {
end++;
}
coef = stoi(s.substr(start, end - start));
}
start = end;
while (end < l) {
if (s[end] >= 'a' && s[end] <= 'z') {
end++;
if (end == l) {
st.push(pair<string, int>(s.substr(start, end - start), 1));
}
}
else if (s[end] >= '0' && s[end] <= '9') {
string temp = s.substr(start, end - start);
start = end;
while (s[end] >= '0' && s[end] <= '9') {
end++;
if (end == l) break;
}
int subcoef = stoi(s.substr(start, end - start));
st.push(pair<string, int>(temp, subcoef));
start = end;
}
else if (s[end] >= 'A' && s[end] <= 'Z' && end > start) {
st.push(pair<string, int>(s.substr(start, end - start), 1));
start = end;
}
else if (s[end] == '(') {
if (end > 0 && (s[end - 1] > '9' || s[end - 1] < '0') && s[end - 1] != '(' && s[end - 1] != ')') {
st.push(pair<string, int>(s.substr(start, end - start), 1));
}
st.push(pair<string, int>("(", 1));
end++;
start = end;
}
else if (s[end] == ')') {
if (end > 0 && (s[end - 1] > '9' || s[end - 1] < '0') && s[end - 1] != '(' && s[end - 1] != ')') {
st.push(pair<string, int>(s.substr(start, end - start), 1));
}
end++;
start = end;
int subcoef = 1;
if (end != l && s[end] >= '0' && s[end] <= '9') {
while (end != l && s[end] >= '0' && s[end] <= '9') {
end++;
}
subcoef = stoi(s.substr(start, end - start));
start = end;
}
vector<pair<string, int> > v;
while (st.top().first != "(") {
pair<string, int> temp = st.top();
st.pop();
temp.second *= subcoef;
v.push_back(temp);
}
st.pop();
for (int i = 0; i < v.size(); i++) {
st.push(v[i]);
}
}
else {
end++;
if (end == l) {
st.push(pair<string, int>(s.substr(start, end - start), 1));
}
}
}
while (!st.empty()) {
pair<string, int> temp = st.top();
st.pop();
temp.second *= coef;
va.push_back(temp);
}
cout << s << ":" << endl;
for (int i = 0; i < va.size(); i++) {
cout << va[i].first << " " << va[i].second << endl;
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
vector<string> left, right;
map<string, int> l, r;
string s;
cin >> s;
analyse(s, left, right);
cout << "leftside:" << endl;
for (int i = 0; i < left.size(); i++) {
vector<pair<string, int> > va;
solve(left[i], va);
for (int j = 0; j < va.size(); j++) {
if (l.count(va[j].first)) l[va[j].first] += va[j].second;
else l.insert(va[j]);
}
}
cout << endl << endl << endl << "rightside:" << endl;
for (int i = 0; i < right.size(); i++) {
vector<pair<string, int> > va;
solve(right[i], va);
for (int j = 0; j < va.size(); j++) {
if (r.count(va[j].first)) r[va[j].first] += va[j].second;
else r.insert(va[j]);
}
}
bool isEqual = true;
for (map<string, int>::iterator p = l.begin(); p != l.end(); p++) {
if (!r.count(p->first)) {
isEqual = false;
break;
}
else {
if (r[p->first] != p->second) {
isEqual = false;
break;
}
}
if (isEqual == false) break;
}
if (isEqual) {
for (map<string, int>::iterator p = r.begin(); p != r.end(); p++) {
if (!l.count(p->first)) {
isEqual = false;
break;
}
else {
if (l[p->first] != p->second) {
isEqual = false;
break;
}
}
if (isEqual == false) break;
}
}
if (isEqual) cout << 'Y' << endl;
else cout << 'N' << endl;
}
}