-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0726-number-of-atoms.cpp
63 lines (60 loc) · 1.73 KB
/
0726-number-of-atoms.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Solution {
public:
string countOfAtoms(string formula) {
map<string, int> mp;
int pos = 0;
mp = func(formula, pos);
string ans;
for (auto& i : mp) {
ans += i.first;
if (i.second != 1) {
ans += to_string(i.second);
}
}
return ans;
}
map<string, int> func(string& str, int& i) {
map<string, int> mp;
string temp;
int n = str.size();
while (i < n) {
if (str[i] == ')') {
i++;
int dig = 0;
while (i < n && str[i] >= '0' && str[i] <= '9') {
dig *= 10;
dig += (str[i] - '0');
i++;
}
for (auto& s : mp) {
s.second *= max(1, dig);
}
return mp;
}
if (i < n && str[i] >= '0' && str[i] <= '9') {
int dig = 0;
while (i < n && str[i] >= '0' && str[i] <= '9') {
dig *= 10;
dig += (str[i++] - '0');
}
mp[temp] += max(1, dig) - 1;
}
if (i < n && str[i] == '(') {
i++;
map<string, int> zz = func(str, i);
for (auto& s : zz) {
mp[s.first] += s.second;
}
}
if (i < n && str[i] >= 'A' && str[i] <= 'Z') {
temp.clear();
temp += str[i++];
while (i < n && str[i] >= 'a' && str[i] <= 'z') {
temp += str[i++];
}
mp[temp]++;
}
}
return mp;
}
};