forked from tony9402/baekjoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (60 loc) · 1.65 KB
/
main.py
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
64
65
66
67
# Authored by : tony9402
# Co-authored by : -
# Link : http://boj.kr/a35de756ecf642b6b2e116f14ffd6093
import sys
def input():
return sys.stdin.readline().rstrip()
s = input()
stack = []
# Check Bracket
for ch in s:
if ch == '(':
stack.append('(')
elif ch == '[':
stack.append('[')
elif ch == ')':
if stack and stack[-1] == '(':
stack.pop(-1)
else:
print(0)
exit(0)
else:
if stack and stack[-1] == '[':
stack.pop(-1)
else:
print(0)
exit(0)
if stack:
print(0)
exit(0)
# [open bracket] + Integer + Integer => [open bracket] + Integer
# compress (add) Integers
def compress():
# Integer를 하나로 합쳐야 하니깐 길이가 2 이상이어야 함.
while len(stack) > 1:
# 두 개의 값이 무조건 Integer이어야 하므로
# Integer면 첫번째 원소가 None으로 되어 있음
a, integer1 = stack[-1]
b, integer2 = stack[-2]
if a or b:
break
stack.pop()
stack.pop()
stack.append((None, integer1 + integer2))
for ch in s:
# open bracket -> append ( open bracket, ~ )
if ch == '(':
stack.append(('(', 2))
elif ch == '[':
stack.append(('[', 3))
elif ch == ')' or ch == ']': # Must Be len(stack) ≥ 1
last1, last2 = stack.pop()
# Case 1 : ~~ open bracket
if last1 != None:
stack.append((None, last2))
# Case 2 : ~~ open bracket, Integer,
else:
a, b = stack.pop()
stack.append((None, last2 * b))
compress()
print(stack[-1][1])