#include <iostream>
#include <cstring>
#include <stack>
#include <vector>
#include <map>
using namespace std;
typedef map<string, int> MPST;
vector<string> split(string& str){//按"+"分割字符串
vector<string> res;
for(int i=0, j; i < str.length(); i = j+1){
j = str.find('+',i);
if(j == string::npos) j = str.length();
res.push_back(str.substr(i, j-i));
}
return res;
}
MPST forge(string& str){
MPST res;
int coe = 1;
if(isdigit(str[0])){
for(int i = 1; ;i ++){
if(!isdigit(str[i])){
coe = stoi(str.substr(0, i));
str = str.substr(i);
break;
}
}
}
int x = 1, sum = 1;
stack<int>num;
/* 数字出现在)后面或字母后面,其后面都要加上x=1*/
for(int i = str.size() - 1; i >= 0; ){
if(isdigit(str[i])){
int j = i;
while(j >= 0 && isdigit(str[j])) j --;
x = stoi(str.substr(j + 1, i - j));
i = j;
}
else if(str[i] == ')'){
i --;
sum *= x;
num.push(x);
x = 1;
}
else if(str[i] == '('){
i --;
sum /= num.top();
num.pop();
}
else{
string key;
if(str[i] >= 'a' && str[i] <= 'z'){
i --;
key = str.substr(i, 2);
}
else key = str[i];
i --;
res[key] += sum * x;
x = 1;
}
}
for(auto it : res)
res[it.first] *= coe;
return res;
}
MPST solve(string& str){
MPST res;
vector<string>expr = split(str);
for(int i = 0; i < expr.size(); i ++){
MPST term = forge(expr[i]);
for(auto it : term) res[it.first] += it.second;
}
return res;
}
int main(){
int T;
cin >> T;
while(T --){
string str;
cin >> str;
int x = str.find('=');
string str1 = str.substr(0, x), str2 = str.substr(x + 1);
MPST left = solve(str1), right = solve(str2);
if(left != right) cout << "N" << endl;
else cout << "Y" << endl;
}
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+2H3PO4=6H2O+Ba3(PO4)2
3Ba(OH)2+2H3PO4=Ba3(PO4)2+6H2O
4Zn+10HNO3=4Zn(NO3)2+NH4NO3+3H2O
4Au+8NaCN+2H2O+O2=4Na(Au(CN)2)+4NaOH
Cu+As=Cs+Au