-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaekJoon2504.cpp
More file actions
52 lines (45 loc) · 869 Bytes
/
BaekJoon2504.cpp
File metadata and controls
52 lines (45 loc) · 869 Bytes
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
//BaekJoon 2504 괄호의 값
#include <iostream>
#include <string>
#include <stack>
using namespace std;
stack<char> stk;
int sum = 0;//result
int mul = 1;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
string input;
cin >> input;
int length = input.length();
for (int i = 0; i < length; i++) {
if (input[i] == '(') {
mul *= 2;
stk.push('(');
}
else if (input[i] == '[') {
mul *= 3;
stk.push('[');
}
else if (input[i] == ')') {
if (input[i - 1] == '(') {
sum += mul;
}
if (stk.empty()) return !printf("0");
if (stk.top() == '(') stk.pop();
mul /= 2;
}
else if (input[i] == ']') {
if (input[i - 1] == '[') {
sum += mul;
}
if (stk.empty()) return !printf("0");
if (stk.top() == '[') stk.pop();
mul /= 3;
}
}
//if not empty return 0
cout << (stk.empty() ? sum : 0);
return 0;
}