#P4008. Efficient Text Editor Simulation
Efficient Text Editor Simulation
Efficient Text Editor Simulation
A long time ago, programmers of DOS3.x grew tired of EDLIN, and they began to create their own text editors. Many years later, by a chance encounter, Xiao Ming discovered one of these early editors. After some simple tests, he was astonished to find that the software could perform tens of thousands of edit operations per second! Determined to build a similar efficient editor, Xiao Ming abstracts the text editor as follows:
- Text: A sequence of zero or more ASCII characters in the closed interval \([32, 126]\).
- Cursor: A marker in the text that indicates a position. It can be at the beginning of the text, at the end, or between any two characters.
- Text Editor: A data structure composed of a text and a cursor within that text (if the text is empty, the editor is said to be empty) supporting the following operations:
Operation Name | Input Format | Function |
---|---|---|
\(\text{Move}(k)\) | Move k | Move the cursor to immediately after the \(k\)-th character (if \(k=0\), move to the beginning of the text). |
\(\text{Insert}(n,s)\) | Insert n s | Insert the string \(s\) of length \(n\) at the current cursor position. The cursor remains unchanged. |
\(\text{Delete}(n)\) | Delete n | Delete the next \(n\) characters after the cursor. The cursor remains unchanged. |
\(\text{Get}(n)\) | Get n | Output the next \(n\) characters after the cursor. The cursor remains unchanged. |
\(\text{Prev}()\) | Prev | Move the cursor one character to the left, if possible. |
\(\text{Next}()\) | Next | Move the cursor one character to the right, if possible. |
Your task is to create an initially empty text editor, process the given operations from the input file, and for every Get
operation output the corresponding substring to the output file.
inputFormat
The first line contains an integer \(Q\) representing the number of operations. Each of the following \(Q\) lines contains one of the following operations:
Move k
Insert n s
(\(n \ge 1\); note thats
is a string of exactly \(n\) characters)Delete n
(\(n \ge 1\))Get n
(\(n \ge 1\))Prev
Next
Operations are given one per line and should be processed in order.
outputFormat
For each Get
operation encountered, output the resulting substring in the order the operations are processed. Each output should be on its own line.
sample
6
Insert 3 abc
Get 2
Move 1
Insert 3 XYZ
Get 4
Get 1
ab
XYZb
X
</p>