-
Notifications
You must be signed in to change notification settings - Fork 0
/
P2504.cpp
54 lines (47 loc) · 1.12 KB
/
P2504.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
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s;
cin >> s;
stack<char> stack;
int temp = 1;
int result = 0;
int flag = 0;
for(int i = 0; i < s.length(); i++) {
if(s[i] == '(') {
stack.push('(');
temp *= 2;
} else if(s[i] == '[') {
stack.push('[');
temp *= 3;
} else if(s[i] == ')') {
if(stack.empty() || stack.top() != '(') {
flag = 1;
break;
}
if (s[i - 1] == '(') {
result += temp;
}
temp /= 2;
stack.pop();
} else if(s[i] == ']') {
if(stack.empty() || stack.top() != '[') {
flag = 1;
break;
}
if (s[i - 1] == '[') {
result += temp;
}
temp /= 3;
stack.pop();
}
}
if(flag || !stack.empty()) cout << "0";
else cout << result;
return 0;
}