forked from tony9402/baekjoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
52 lines (43 loc) · 1.29 KB
/
main.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
// Authored by : tallua_y
// Co-authored by :
// Link : http://boj.kr/48252863d9ae4860b54cfceb442110ba
#include <bits/stdc++.h>
using namespace std;
struct Token {
int value;
size_t pos;
};
int main(int argc, char** argv)
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
string input;
cin >> input;
vector<Token> tokens;
tokens.push_back({ 0, '$' });
for (size_t head = 0; head < input.size(); ++head) {
if (input[head] == '(' || input[head] == '[') {
tokens.push_back({ 0, head });
} else {
const auto open_tag = (input[head] == ')' ? '(' : '[');
if (tokens.empty() || input[tokens.back().pos] != open_tag) {
tokens.clear();
break;
}
Token token = tokens.back();
tokens.pop_back();
const auto designated_value = (input[head] == ')' ? 2 : 3);
if (token.pos + 1 == head) {
tokens.back().value += designated_value;
} else {
tokens.back().value += token.value * designated_value;
}
}
}
if (tokens.size() != 1) {
cout << "0\n";
} else {
cout << tokens.back().value << '\n';
}
return 0;
}