#include <bits/stdc++.h>
using namespace std;
unordered_map<string, long long> xm; // 相对分子质量。
string s;
int len;
void build() {
bool f = 1;
int op = 0;
string x = "";
for (int i = 1; i <= len; i++) {
// cout << s[i] << "-"; ///
if (s[i] == ' ') {
f = 0;
} else if (f) {
x += s[i];
// cout << "\nop\n";
} else {
op = op * 10 + s[i] - '0';
}
}
xm[x] = op;
// cout << ">" << x << "=" << op << "\n"; ///
return;
}
struct Node {
long long type; // 1:分子相对质量(或者之和) 2:( 其实不会有的:3:) 4:乘号
long long num;
long long times;
};
stack<Node> st;
void solve() {
while (!st.empty()) {
st.pop();
}
for (int i = 1; i <= len; i++) {
if (s[i] >= 'A' && s[i] <= 'Z') {
string x = "";
x += s[i];
int j = i + 1;
for (; j <= len; j++) {
if (!(s[j] >= 'a' && s[j] <= 'z')) {
j--;
break;
}
x += s[j];
}
j++;
long long times = 0;
for (; i <= len; j++) {
if (!(s[j] >= '0' && s[j] <= '9')) {
j--;
break;
}
times = times * 10 + s[j] - '0';
}
// i = j + 1;
i = j;
if (times == 0) {
times = 1;
}
// cout << x << times << "==\n"; ///////////////////////
st.push((Node){1, xm[x] * times, 1});
if (xm[x] == 0) {
st.push((Node){0, 0, 0});
// cout << "UNKNOWN\n";
return;
}
// i = j + 1;
} else if (s[i] == ')') {
long long num = 0;
while (!st.empty()) {
if (st.top().type == 2) {
st.pop();
break;
}
num += st.top().num;
st.pop();
}
//////
int j = i + 1;
long long times = 0;
for (; i <= len; j++) {
if (!(s[j] >= '0' && s[j] <= '9')) {
j--;
break;
}
times = times * 10 + s[j] - '0';
}
if (times == 0) {
times = 1;
}
// i = j + 1;
i = j;
// cout << num << " " << times << "==\n"; ///////////////////////
long long op = num * times;
st.push((Node){1, op, 1});
} else if (s[i] == '(') {
st.push((Node){2, 0, 0});
}
}
}
bool f;
int main() {
while (getline(cin, s)) {
if (s == "0") {
break;
} else if (s == "END_OF_FIRST_PART") {
f = 1;
continue;
} else if (f == 0) {
len = s.size();
s = " " + s;
build();
} else if (f == 1) {
s = " (" + s + ")";
len = s.size() - 1;
solve();
if (st.empty() || st.top().num == 0) {
cout << "UNKNOWN\n";
continue;
}
cout << st.top().num << "\n";
}
}
// cout << xm["Cc"] << "\n";
return 0;
}
/*
H 1
He 4
C 12
O 16
F 19
Ne 20
Cu 64
Cc 333
END_OF_FIRST_PART
H2C
(MgF)2As
Cu(OH)2
H((CO)2F)99
0
14
UNKNOWN
98
7426
*/
全 WA。。。。。。
求助大佬,求调,过样例,但是 Wrong Answer.wrong answer Too short on line 1.。。。
感谢大佬。