본문 바로가기
python-algorithm

백준 4949 균형잡힌 세상

by 무적김두칠 2022. 11. 9.

https://www.acmicpc.net/problem/4949

 

4949번: 균형잡힌 세상

하나 또는 여러줄에 걸쳐서 문자열이 주어진다. 각 문자열은 영문 알파벳, 공백, 소괄호("( )") 대괄호("[ ]")등으로 이루어져 있으며, 길이는 100글자보다 작거나 같다. 각 줄은 마침표(".")로 끝난다

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
while True:
    answer = 'yes'
    s=input()
    if s=='.':
        break
    stack=[]
    for i in s:
        if i=='(' or i=='[':
            stack.append(i)
        if i==')':
            if len(stack) != 0 and stack[-1=='(':
                stack.pop()
            else:
                stack.append(i)
        elif i==']':
            if len(stack) != 0 and stack[-1=='[':
                stack.pop()
            else:
                stack.append(i)
 
    if len(stack) != 0:
        answer ="no"
    print(answer)
cs

 

스택을 사용하는 문제고 조건문을 합치려고 괄호들 아스키코드를 보니 합치기어려워 그냥 쪼갰다..

Using Stack, First time I think just one if clause, and Find those ascii code of brackets.. but not easy
And then just two if clause

print(ord('('))
print(ord(')'))
print(ord('['))
print(ord(']'))

반응형

'python-algorithm' 카테고리의 다른 글

백준 18258 큐 2  (0) 2022.11.09
백준 1620 나는야 포켓몬 마스터 이다솜  (0) 2022.11.09
백준 18398 HOMWRK  (0) 2022.11.09
백준 1966 프린터 큐  (0) 2022.11.08
백준 10816 숫자 카드 2  (0) 2022.11.08

댓글