// Problem: P4711 「化学」相对分子质量
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4711
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
const int maxn = 1e6 + 10;
char str[maxn];
int len, H2O = 18;
double ans;
map<string, double> s;
double get_ans(int l, int r)
{
double res = 0;
for (int i = l; i <= r;) {
if (str[i] == '(') {
int j = i + 1;
for (; j <= r; j++)
if (str[j] == ')')
break;
double tmp = get_ans(i + 1, j - 1);
if (str[j + 1] == '_') {
int num = -1;
for (j = j + 3; j <= r; j++) {
if (str[j] == '}')
break;
if (num == -1)
num = 0;
num = num * 10 + (str[j] - '0');
}
res += 1.0 * (num == -1 ? 1 : num) * tmp;
} else {
res += tmp;
}
i = j + 1;
} else if (str[i] != ')') {
int j = i + 1;
string ele = "";
ele += str[i];
for (; j <= r; j++) {
if ((str[j] >= 'A' && str[j] <= 'Z') || str[j] == '_' || str[j] == '(')
break;
ele += str[j];
}
if (str[j] == '_') {
int num = -1;
for (j = j + 2; j <= r; j++) {
if (str[j] == '}')
break;
if (num == -1)
num = 0;
num = num * 10 + (str[j] - '0');
}
res += 1.0 * (num == -1 ? 1 : num) * s[ele];
i = j + 1;
} else
res += 1.0 * s[ele], i = j;
}
}
return res;
}
int main()
{
s["H"] = 1;
s["C"] = 12;
s["N"] = 14;
s["O"] = 16;
s["F"] = 19;
s["Na"] = 23;
s["Mg"] = 24;
s["Al"] = 27;
s["Sl"] = 28;
s["P"] = 31;
s["S"] = 32;
s["Cl"] = 35.5;
s["K"] = 39;
s["Ca"] = 40;
s["Mn"] = 55;
s["Fe"] = 56;
s["Cu"] = 64;
s["Zn"] = 65;
s["Ag"] = 108;
s["I"] = 127;
s["Ba"] = 137;
s["Hf"] = 178.5;
s["Pt"] = 195;
s["Au"] = 197;
s["Hg"] = 201;
cin >> (str + 1);
len = strlen(str + 1);
for (int i = 1; i <= len; i++) {
if (str[i] == '~') {
int num = -1;
for (int j = i + 1; j <= len - 6; j++) {
if (num == -1)
num = 0;
num = num * 10 + (str[j] - '0');
}
ans += 1.0 * H2O * (num == -1 ? 1 : num);
ans += 1.0 * get_ans(1, i - 1);
break;
}
}
if (ans)
cout << ans << "\n";
else {
ans = get_ans(1, len);
cout << ans << "\n";
}
return 0;
}
我知道我菜。别骂了。