SMALL
문제


코드
import sys
input = sys.stdin.readline
if __name__ == "__main__":
left = list(input().strip())
right = []
n = int(input())
for _ in range(n):
command = input().split()
if command[0] == "L":
if left:
right.append(left.pop())
elif command[0] == "D":
if right:
left.append(right.pop())
elif command[0] == "B":
if left:
left.pop()
else:
left.append(command[1])
print(''.join(left + right[::-1]))
left는 커서 왼쪽, right는 커서 오른쪽을 저장한다.
예를 들어 ab|c 상태라면 내부는 다음과 같다.
left = ['a', 'b']
right = ['c']
그런데 커서를 여러 번 움직이면 right는 스택이라 거꾸로 쌓일 수 있어서, 출력할 때는 반드시 뒤집어서 붙여야 한다.
right[::-1]
LIST
'CODE > Algorithm (Python)' 카테고리의 다른 글
| BOJ 13335 트럭 (0) | 2026.04.28 |
|---|---|
| BOJ 2161 카드1 (0) | 2026.04.28 |
| BOJ 10799 쇠막대기 (0) | 2026.04.28 |
| BOJ 3986 좋은 단어 (0) | 2026.04.28 |
| BOJ 1544 사이클 단어 (0) | 2026.04.28 |