
백준 1406번은 간단한 에디터를 구현하는 문제였다.
처음에는 배열과 스택 하나만으로 해결하려고 했는데, 잘 안되어서 구글링해서 해결했다. 두개의 스택을 이용해서 커서와 같이 구현을 해놓은 코드를 이용했다.
import sys
stack_l = list(input())
stack_r = []
n = int(input())
for i in range(n):
command = sys.stdin.readline().split()
if command[0] == "L" and stack_l:
stack_r.append(stack_l.pop())
elif command[0] == "D" and stack_r:
stack_l.append(stack_r.pop())
elif command[0] == "B" and stack_l:
stack_l.pop()
elif command[0] == "P":
stack_l.append(command[1])
print("".join(stack_l + list(reversed(stack_r))))
'알고리즘' 카테고리의 다른 글
[Algorithms] 백준 1966번: 프린터 (0) | 2023.08.14 |
---|---|
[Algorithms] 백준 1874번: 스택 수열 (0) | 2023.08.14 |
[Algorithms] 백준 1012번: 유기농 배추 (0) | 2023.08.13 |
[Algorithms] 파이썬 알고리즘 인터뷰 #6, Leetcode 5번: Longest Palindromic Substring (0) | 2021.03.30 |
[Algorithms] 파이썬 알고리즘 인터뷰 #5, LeetCode 49번: Group Anagrams (0) | 2021.03.28 |