본문 바로가기

알고리즘/백준

[백준 1406][파이썬] 에디터

https://www.acmicpc.net/problem/1406

 

1406번: 에디터

첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수

www.acmicpc.net

 

import sys

input = sys.stdin.readline

L_stack = list(input().strip())
R_stack = []
M = int(input())

for i in range(M):
  command = input().strip().split()

  if command[0] == 'L' and len(L_stack) != 0:
    R_stack.append(L_stack.pop())
  elif command[0] == 'D' and len(R_stack) != 0:
    L_stack.append(R_stack.pop())
  elif command[0] == 'B' and len(L_stack) != 0:
    L_stack.pop()
  elif command[0] == 'P':
    L_stack.append(command[1])

R_stack.reverse()
print("".join(L_stack)+ "".join(R_stack))