본문 바로가기
알고리즘/백준

백준-1874번(스택 수열)-python3

by nyeongha 2023. 11. 24.
import sys
arr = [int(sys.stdin.readline()) for _ in range(int(sys.stdin.readline()))]
stack = []
stack2 = []
a = 1
b = 0
flag = True
while b != len(arr):
    if (stack2 and stack2[-1] == arr[b]) and arr[b] in stack2:
        stack.append('-')
        stack2.pop()
        b += 1
    elif stack2 and arr[b] in stack2 and stack2[-1] != arr[b]:
        flag = False
        break
    elif not stack2 or (stack2 and stack2[-1] != arr[b]):
        stack.append('+')
        stack2.append(a)
        a += 1
    elif stack2 and a == arr[b]:
        stack.append('+')
        stack2.append(a)
        stack.append('-')
        stack2.pop()
        a += 1
        b += 1


if stack2 or flag == False:
    print('NO')
else:
    print('\n'.join(stack[0:]))




'알고리즘 > 백준' 카테고리의 다른 글

백준-1912번(연속합)-python3  (0) 2023.11.24
백준-1904번(01타일)-python3  (0) 2023.11.24
백준-1789번(수들의 합)-python3  (0) 2023.11.24
백준-1764번(듣보잡)-python3  (0) 2023.11.24
백준-1759번(암호 만들기)-python3  (0) 2023.11.24