알고리즘(python)/자료구조

[Python]Stack 백준 1874

개발일기 2019. 12. 15. 02:01
반응형
문제
https://www.acmicpc.net/problem/1874


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
#초기 설정
n=int(input())
stack=[]
print_list=[]
i=1
 
 
while(n>0):
    n -= 1
    sequence=int(input())
    #스택이 비어있다면 하나 채워준다
    if len(stack)==0:
        stack.append(i)
        print_list.append("+")
        i += 1
 
    while(stack[len(stack)-1]<sequence):#스택의 탑보다 sequence가 크면
        stack.append(i)
        print_list.append("+")
        i += 1
    if stack[len(stack)-1]==sequence: #같다면 pop
        stack.pop()
        print_list.append("-")
    elif stack[len(stack) - 1> sequence: #스택의 탑보다 sequence가 작다면 불가능
        print_list = []
        print_list.append("NO")
        break
 
 
 
for i in print_list:
    print(i)
 

cs


스택을 가장 잘보여주는 문제 인거 같다.

스택을 이해하고 있다면 간단히 풀 수 있을것이다.



반응형