연결리스트 구현 연습으로 풀었다
입출력이 많을 때는 Pypy3
보다 python3
이 조금 더 빠르다고한다
혹시 50~52%에서 틀린다면 이부분을 고려해보길...
import sys
#sys.stdin = open('input.txt', 'r')
input = sys.stdin.readline
class Node:
def __init__(self, data='$'):
self.data = data
self.pre = None
self.nxt = None
class DLL:
def __init__(self):
self.head = Node('HEAD')
self.tail = Node('TAIL')
self.head.nxt = self.tail
self.tail.pre = self.head
self.cur = self.tail
def append(self, data):
node = Node(data=data)
node.pre = self.cur.pre
self.cur.pre.nxt = node
self.cur.pre = node
node.nxt = self.cur
def pop(self):
if self.cur.pre == self.head:
return
self.cur.pre.pre.nxt = self.cur
self.cur.pre = self.cur.pre.pre
def move_left(self):
if self.cur.pre != self.head:
self.cur = self.cur.pre
def move_right(self):
if self.cur != self.tail:
self.cur = self.cur.nxt
def __str__(self):
ret = ''
node = self.head.nxt
while node != self.tail:
ret += str(node.data)
node = node.nxt
return ret
dll = DLL()
S = input().rstrip()
for s in S:
dll.append(s)
N = int(input())
for _ in range(N):
cmd = input().rstrip()
if cmd == 'L':
dll.move_left()
elif cmd == 'D':
dll.move_right()
elif cmd == 'B':
dll.pop()
else:
_, data = cmd.split()
dll.append(data)
print(dll)