[ 2023-06-01 ⛱️ TIL ]

Burkey·2023년 6월 1일
0

TIL

목록 보기
91/157

백준 5397번


import sys

input = sys.stdin.readline

n = int(input())

for _ in range(n):
    S = input().rstrip()
    s_li_l = [] 
    # s_li_l 배열에 끝부분이 커서의 위치
    s_li_r = []
    for i in range(len(S)):
        if s_li_l and S[i] == '<':
            s = s_li_l.pop()
            s_li_r.append(s)
        elif s_li_r and S[i] == ">":
            s = s_li_r.pop()
            s_li_l.append(s)
        elif s_li_l and S[i] == "-":
            s_li_l.pop() # 커서의 왼쪽 글자 삭제
        elif S[i] != '>' and S[i] != '<' and S[i] != "-":
            s_li_l.append(S[i])

    s_li_l += reversed(s_li_r)
    # s_li_l의 마지막 부터 stack이 쌓여
    # 마지막에 reversed 헤야 한다.
    
    print(''.join(s_li_l))

스택이 자료구조 문제 중에서 가장 좋은것 같다. ㅋㅋ
가장 잘 풀린다.. ㅋㅋㅋㅋㅋㅋ
한동안 스택 문제만 풀어볼 예정

profile
스탯 올리는 중

0개의 댓글