-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path27.cpp
63 lines (53 loc) · 1.24 KB
/
27.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
// O(n)
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <iomanip>
using namespace std;
#define FILE_IO {freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);}
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(NULL), cout.tie(NULL)
inline int convert(char &c) {
if (c == 'C') return 12;
if (c == 'H') return 1;
if (c == 'O') return 16;
if (c == '(') return 0;
if (c == ')') return -1;
return (int) (c - '0');
}
int main() {
FAST_IO;
// FILE_IO;
string fo; cin >> fo;
stack<int> s;
int res = 0;
for (int c, sm, top, i = 0; i < fo.size(); ++i) {
c = convert(fo[i]);
if (c >= 2 && c <= 9) {
top = s.top();
s.pop();
s.push(top * c);
} else if (c == -1) {
sm = 0;
while (true) {
top = s.top();
s.pop();
sm += top;
if (top == 0) {
s.push(sm);
break;
}
}
} else {
s.push(c);
}
}
while (!s.empty()) {
res += s.top();
s.pop();
}
cout << res;
return 0;
}