백준 1406 에디터

김민영·2023년 1월 14일
0

알고리즘

목록 보기
70/125

과정

  • 스택을 두 개 만든다.
  • 하나의 스택에는 커서 기준 앞의 내용을 담고, 다른 스택에는 커서 기준 뒤의 내용을 담는다.
  • 출력 시, 커서 뒤의 내용을 앞에 더해서 출력한다.
  • 입력 속도 향상을 위해 sys.stdin.readline을 사용한다.
import sys
input = sys.stdin.readline

lst = list(input().rstrip())
stack = []
N = int(input())
for _ in range(N):
    inps = list(input().rstrip().split())
    if inps[0] == "P":
        lst.append(inps[1])
    elif inps[0] == "L":
        if lst:
            stack.append(lst.pop())
    elif inps[0] == "D":
        if stack:
            lst.append(stack.pop())
    elif inps[0] == "B":
        if lst:
            lst.pop()
for _ in range(len(stack)):
    lst.append(stack.pop())
for i in lst:
    print(i, end="")
profile
노션에 1차 정리합니당 - https://cream-efraasia-f3c.notion.site/4fb02c0dc82e48358e67c61b7ce8ab36?v=

0개의 댓글