Notice
														
												
											
												
												
													Recent Posts
													
											
												
												
													Recent Comments
													
											
												
												
													Link
													
											
									| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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 | 
													Tags
													
											
												
												- 소트
 - sort
 - 소트인사이드
 - 404에러
 - 퇴각검색
 - Backtracking
 - 덩치
 - 다익스트라
 - 스택
 - 스프링프레임워크
 - 백준
 - 최빈값
 - 코테
 - 프로그래머스
 - 2중포문
 - 그리디
 - 동적계획법
 - DFS
 - 파이썬
 - 코딩테스트
 - 정렬
 - 월간 코드 챌린지 시즌2
 - 스프링
 - 통계학
 - 동적
 - 백트래킹
 - Python
 - 최단거리
 - 그리디알고리즘
 - 브루트포스
 
													Archives
													
											
												
												- Today
 
- Total
 
취준생의 코딩테스트 연습기
[백준] 2504번 괄호의 값 / 파이썬(python) 본문
# 문제 링크
https://www.acmicpc.net/problem/2504
2504번: 괄호의 값
4개의 기호 ‘(’, ‘)’, ‘[’, ‘]’를 이용해서 만들어지는 괄호열 중에서 올바른 괄호열이란 다음과 같이 정의된다. 한 쌍의 괄호로만 이루어진 ‘()’와 ‘[]’는 올바른 괄호열이다. 만일
www.acmicpc.net
# Soultion
여러가지 경우의 수를 잘 생각해야 하는 문제이다.
나는 test case가 '(()([())])'인 경우에 0이 나와야하는데 28이 나오는 반례를 해결했다.
| 
 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 
 | 
 s = input() 
stack = [] 
for i in s: 
    if i in ['(','[']: 
        stack.append(i) 
    else:  
        if stack and i==')': 
            if len(stack)>1 and str(stack[-1]).isdigit(): 
                n = stack.pop()*2 
                stack.pop() 
                stack.append(n) 
            elif stack[-1]=='(': 
                stack.pop() 
                stack.append(2) 
            else: 
                stack.append('-') 
                break 
        elif stack and i==']': 
            if len(stack)>1 and str(stack[-1]).isdigit(): 
                n = stack.pop()*3 
                stack.pop() 
                stack.append(n) 
            elif stack[-1]=='[': 
                stack.pop() 
                stack.append(3) 
            else: 
                stack.append('-') 
                break 
        else: 
            stack.append('-') 
            break 
    if len(stack)>1 and str(stack[-1]).isdigit() and str(stack[-2]).isdigit(): 
        stack.append(stack.pop()+stack.pop()) 
if len(stack)==1 and str(stack[-1]).isdigit(): 
    print(stack.pop()) 
else: 
    print(0) 
 | 
cs | 
'코딩테스트 > 백준' 카테고리의 다른 글
| [백준] 2606번 바이러스 / 파이썬(python) (2) | 2021.05.18 | 
|---|---|
| [백준] 1260번 DFS와 BFS / 파이썬(python) (0) | 2021.05.17 | 
| [백준] 14888번 연산자 끼워넣기 / 파이썬(python) (0) | 2021.05.13 | 
| [백준] 13305번 주유소 / 파이썬(python) (0) | 2021.04.11 | 
| [백준] 1541번 잃어버린 괄호 / 파이썬(python) (0) | 2021.04.09 | 
			  Comments