就WA了#13一个点,拼劲全力无法战胜,讨论区里的hack全部通过,求调
#include <bits/stdc++.h>
using namespace std;
map<string, double> w;
string s;
double ans = 0.0;
void init() { w["H"] = 1, w["C"] = 12, w["N"] = 14, w["O"] = 16, w["F"] = 19, w["Na"] = 23, w["Mg"] = 24, w["Al"] = 27, w["Si"] = 28, w["P"] = 31, w["S"] = 32, w["Cl"] = 35.5, w["K"] = 39, w["Ca"] = 40, w["Mn"] = 55, w["Cu"] = 64, w["Zn"] = 65, w["Ag"] = 108, w["I"] = 127, w["Ba"] = 137, w["Hf"] = 178.5, w["Pt"] = 195, w["Au"] = 197, w["Hg"] = 201; }
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
init();
cin >> s;
string tmp = "";
double num = 1, cnt = 0, ad, tot = 0;
bool op = false;
for (int i = 0; i < s.size(); i++)
{
char t = s[i];
if (s[i - 1] == '~')
{
if (!tmp.empty())
ans += num * w[tmp];
if (isdigit(s[i]))
{
string tt = "";
while (isdigit(s[i]))
tt.push_back(s[i++]);
stringstream ss(tt);
ad = 0, ss >> ad;
}
else
ad = 1;
cout << ans * 1.0 + ad * 18.0;
return 0;
}
if (isupper(t))
{
if (!tmp.empty())
{
if (!op)
ans += num * w[tmp];
else
cnt += num * w[tmp];
num = 1, tmp.clear();
}
tmp.push_back(t);
}
if (islower(t))
tmp.push_back(t);
if (t == '_')
{
i += 2;
string tt = "";
while (s[i] != '}')
tt.push_back(s[i++]);
stringstream ss(tt);
num = 0, ss >> num;
}
if (t == '(')
{
op = true;
if (!tmp.empty())
ans += w[tmp] * num, num = 1, tmp.clear();
}
if (t == ')')
{
op = false;
if (!tmp.empty())
cnt += num * w[tmp], num = 1, tmp.clear();
if (s[i + 1] != '_')
ans += cnt, cnt = 0;
else
{
i += 3;
string tt = "";
while (s[i] != '}')
tt.push_back(s[i++]);
stringstream ss(tt);
num = 0, ss >> num;
ans += cnt * num, num = 1, cnt = 0;
}
}
}
if (!tmp.empty())
ans += w[tmp] * num;
cout << ans;
return 0;
}