#C11587. Text Editor Simulation
Text Editor Simulation
Text Editor Simulation
You are given the task of simulating a simple text editor that supports a limited set of operations. The editor initially contains an empty text and a cursor that is positioned before the first character. The editor supports the following operations:
- insert x: Insert character x immediately after the current cursor position, then move the cursor to this newly inserted character.
- delete: Delete the character immediately after the current cursor position. If there is no character after the cursor, do nothing.
- moveLeft: Move the cursor one position to the left. If the cursor is at the beginning, do nothing.
- moveRight: Move the cursor one position to the right. If the cursor is at the end, do nothing.
- print: Print the current content of the text in a single line.
- END: Indicates the end of input.
The underlying idea is to simulate the text editing using a data structure (such as a doubly linked list) which allows for efficient insertion and deletion. In many implementations a dummy node is used to simplify operations at the beginning of the text.
Recall that if we denote the cursor position by an index i (starting at -1, representing the position before the first character), then an insert
operation inserts the character at index i+1
and updates the cursor to that new position.
inputFormat
The input is given via standard input (stdin). It consists of multiple lines. Each line contains one command which can be one of the following:
insert x
wherex
is a single character.delete
moveLeft
moveRight
print
END
which indicates the end of the input.
There can be multiple print
commands. The commands are processed sequentially.
outputFormat
For each print
command encountered in the input, output the current state of the text on a new line via standard output (stdout). The text should be printed exactly as it appears in the editor (with no extra spaces or characters).
insert a
print
END
a
</p>