1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import sys
sys.stdin = open("input", "rt")
code=input()
stack=[]
piece=0
for i in range(len(code)):
if code[i]=='(': # '(' 면 stack에 쌓아주고
stack.append(code[i])
else:
if code[i-1]=='(': # '()' 면 레이저 발사
stack.pop() # 스택에 쌓인 막대 개수만큼 조각이 난다
piece+=len(stack)
else:
stack.pop() # '))' 면 막대 하나가 끝남
piece+=1 # 따라서 마지막의 1조각 더해준다
print(piece)
|
cs |
728x90
'Data Structures & Algorithms' 카테고리의 다른 글
[Algorithms] 스택,큐,해쉬,힙-공주 규하기(큐) (0) | 2022.05.18 |
---|---|
[Algorithms] 스택,큐,해쉬,힙-후위 표기식 만들기(스택) (0) | 2022.05.18 |
[Algorithms] 스택,큐,해쉬,힙-가장 큰 수(스택) (0) | 2022.05.17 |
[Algorithms] 결정&그리디 알고리즘-역수열(그리디 알고리즘) (0) | 2022.05.17 |
[Algorithms] 결정&그리디 알고리즘-침몰하는 타이타닉(그리디 알고리즘) (0) | 2022.05.16 |