Skip to content

Commit 2a61a9d

Browse files
committed
solve: 4949 균형잡힌 세상
1 parent b5f5c3d commit 2a61a9d

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
"""
3+
문제 이름: 균형잡힌 세상
4+
문제 번호: 4949
5+
문제 링크: https://www.acmicpc.net/problem/4949
6+
난이도: Silver IV
7+
태그: 자료 구조, 스택, 문자열
8+
"""
9+
import sys
10+
11+
12+
def input(): return sys.stdin.readline().rstrip()
13+
14+
15+
parentheses = {
16+
')': '(',
17+
']': '[',
18+
}
19+
open_parentheses = parentheses.values()
20+
close_parentheses = parentheses.keys()
21+
22+
23+
def solve(text: str) -> bool:
24+
st = []
25+
for c in text:
26+
if c in open_parentheses:
27+
st.append(c)
28+
29+
elif c in close_parentheses:
30+
if len(st) > 0 and st[-1] == parentheses[c]:
31+
st.pop()
32+
else:
33+
return False
34+
35+
return len(st) == 0
36+
37+
38+
while True:
39+
text = input()
40+
41+
if text == '.':
42+
break
43+
44+
print("yes" if solve(text) else "no")
45+
46+
# 108ms
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
file: "4949.md"
3+
name: "균형잡힌 세상"
4+
src: "https://www.acmicpc.net/problem/4949"
5+
tags:
6+
- 자료 구조
7+
- 스택
8+
- 문자열
9+
done: true
10+
draft: false
11+
level: 7
12+
difficulty: "Silver IV"
13+
date: 2021-11-06
14+
---
15+
16+
# 균형잡힌 세상
17+
18+
```python
19+
import sys
20+
21+
def input(): return sys.stdin.readline().rstrip()
22+
23+
parentheses = {
24+
')': '(',
25+
']': '[',
26+
}
27+
open_parentheses = parentheses.values()
28+
close_parentheses = parentheses.keys()
29+
30+
def solve(text: str) -> bool:
31+
32+
# 괄호 갯수가 맞지 않을경우
33+
st = []
34+
for c in text:
35+
# 여는 괄호 일경우 push
36+
if c in open_parentheses:
37+
st.append(c)
38+
39+
elif c in close_parentheses:
40+
# 닫는괄호 단독으로 나오거나 대응하는 괄호가 매칭되지 않을경우 break
41+
if len(st) > 0 and st[-1] == parentheses[c]:
42+
st.pop()
43+
else:
44+
return False
45+
46+
return len(st) == 0
47+
48+
49+
while True:
50+
text = input()
51+
52+
if text == '.':
53+
break
54+
55+
print("yes" if solve(text) else "no" )
56+
57+
# 108ms
58+
```

0 commit comments

Comments
 (0)