초기에 편집기에 입력되어 있는 문자열이 주어지고, 그 이후 입력한 명령어가 차례로 주어졌을 때, 모든 명령어를 수행하고 난 후 편집기에 입력되어 있는 문자열을 구하는 프로그램을 작성하시오. 단, 명령어가 수행되기 전에 커서는 문장의 맨 뒤에 위치하고 있다고 한다.
첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수를 나타내는 정수 M(1 ≤ M ≤ 500,000)이 주어진다. 셋째 줄부터 M개의 줄에 걸쳐 입력할 명령어가 순서대로 주어진다. 명령어는 위의 네 가지 중 하나의 형태로만 주어진다.
첫째 줄에 모든 명령어를 수행하고 난 후 편집기에 입력되어 있는 문자열을 출력한다.
import sys
inputs = list(sys.stdin.readline().strip())
commands = list(map(lambda x : x[:-1], sys.stdin.readlines()[1:]))
class Editor:
def __init__(self, line):
self.pointer = len(line);
self.line = line;
def L(self):
if self.pointer > 0:
self.pointer -= 1
def D(self):
if self.pointer < len(self.line):
self.pointer += 1;
def B(self):
if self.pointer > 0:
del self.line[self.pointer-1];
self.pointer -= 1;
def P(self, x):
self.line.insert(self.pointer, x);
self.pointer += 1;
e = Editor(inputs);
for command in commands:
command = command.split()
if (command[0] == "L"): e.L()
elif (command[0] == "D"): e.D()
elif (command[0] == "B"): e.B()
else: e.P(command[1]);
print(''.join(e.line))
import sys
inputs = sys.stdin.readline().strip()
commands = list(map(lambda x : x[:-1], sys.stdin.readlines()[1:]))
class Editor:
def __init__(self, line):
self.pointer = len(line);
self.line = line;
def L(self):
if self.pointer > 0:
self.pointer -= 1
def D(self):
if self.pointer < len(self.line):
self.pointer += 1;
def B(self):
if self.pointer > 0:
frontLine = self.line[:self.pointer-1]
rearLine = self.line[self.pointer:]
newLine = frontLine + rearLine
self.line = newLine
self.pointer -= 1;
def P(self, x):
frontLine = self.line[:self.pointer] + x
rearLine = self.line[self.pointer:]
newLine = frontLine + rearLine
self.line = newLine
self.pointer += 1;
e = Editor(inputs);
for command in commands:
command = command.split()
if (command[0] == "L"): e.L()
elif (command[0] == "D"): e.D()
elif (command[0] == "B"): e.B()
else: e.P(command[1]);
print(e.line)
import sys
inputs = list(sys.stdin.readline().strip())
commands = list(map(lambda x : x[:-1], sys.stdin.readlines()[1:]))
class Editor:
def __init__(self, line):
self.frontStack = line
self.rearStack = []
def L(self):
if len(self.frontStack) > 0:
self.rearStack.append(self.frontStack.pop());
def D(self):
if len(self.rearStack) > 0:
self.frontStack.append(self.rearStack.pop());
def B(self):
if len(self.frontStack) > 0:
del self.frontStack[-1]
def P(self, x):
self.frontStack.append(x);
e = Editor(inputs);
for command in commands:
command = command.split()
if (command[0] == "L"): e.L()
elif (command[0] == "D"): e.D()
elif (command[0] == "B"): e.B()
else: e.P(command[1]);
print(''.join(e.frontStack+e.rearStack[::-1]))