본문 바로가기

백준문제풀이

[백준 문제풀이] 1406번 : 에디터

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

 

1406번: 에디터

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

www.acmicpc.net

💡 split으로 풀다가 개고생..

 

1. 소스코드

import sys
N=list(sys.stdin.readline().strip())
M=int(sys.stdin.readline())
a=[]

for i in range(M):
    word=sys.stdin.readline().strip()
    if word[0]=="P":
        N.append(word[2])
    elif word[0]=="L" and N!=[]:
        a.append(N.pop())
    elif word[0]=="D" and a!=[]:
        N.append(a.pop())        
    elif word[0]=="B" and N!=[]:
        N.pop()

print("".join(N+list(reversed(a))))