카테고리 없음
[BOJ] 1406번 : 에디터
twoDeveloper
2021. 9. 11. 22:56
■ 문제풀이
· 'L' : 왼쪽으로 한 칸 옮김 => 옮길 시 string의 top 값을 cursor 스택에 append | string 리스트는 비어있으면 안됨
· 'D' : 오른쪽으로 한 칸 옮김 => cursor의 top 값을 string 스택에 append | cursor 리스트는 비어있으면 안됨
· 'B' : 문자를 삭제 => string의 top 값을 pop
· 'P' : 문자 추가 => string에 추가할 문자를 append
· cursor 스택 값을 reversed 시킴
■ 문제해답
import sys
input = sys.stdin.readline
string = list(input().strip())
cursor = []
n = int(input())
for i in range(n):
m = input().split()
if m[0] == 'L' and string != []:
cursor.append(string.pop())
elif m[0] == 'D' and cursor != []:
string.append(cursor.pop())
elif m[0] == 'B' and string != []:
string.pop()
elif m[0] == 'P':
string.append(m[1])
print("".join(string + list(reversed(cursor))))
문제출처: https://www.acmicpc.net/problem/1406