P4008 [NOI2003] Text Editor
Description
A long time ago, programmers using DOS 3.x grew tired of `EDLIN`. So, people started switching to their own text editors...
Many years later, by chance, Xiaoming found one of those editors. After running some simple tests, he was surprised to find that the software could perform over ten thousand editing operations per second (of course, you cannot perform such a test by hand). Xiaoming became determined to build something similar. Can you help him?
To clarify the goal, Xiaoming gives an abstract definition of a "text editor":
Text: A sequence consisting of $0$ or more characters whose ASCII codes lie in the closed interval $\left[32,126\right]$.
Cursor: A marker indicating a position within a text. It can be at the beginning of the text, at the end of the text, or between any two characters.
Text editor: A data structure composed of a piece of text and a cursor within the text, supporting the following operations. If the text is empty, we say the text editor is empty.
| Operation name | Format in input file | Function |
| :------------------------ | :------------------- | :------------------------------------------------------------------------ |
| $\text{Move}(k)$ | Move k | Move the cursor to the position after the $k$-th character; if $k = 0$, move the cursor to the beginning of the text. |
| $\text{Insert}(n,s)$ | Insert n s | Insert the string $s$ of length $n$ at the cursor. The cursor position does not change. Guarantee that $n \geq 1$. |
| $\text{Delete}(n)$ | Delete n | Delete the $n$ characters after the cursor. The cursor position does not change. Guarantee that $n \geq 1$. |
| $\text{Get}(n)$ | Get n | Output the $n$ characters after the cursor. The cursor position does not change. Guarantee that $n \geq 1$. |
| $\text{Prev}()$ | Prev | Move the cursor one character to the left. |
| $\text{Next}()$ | Next | Move the cursor one character to the right. |
Your task is:
- Build an empty text editor.
- Read a sequence of operations from the input file and execute them.
- For every executed `GET` operation, write the specified content to the output file.
Input Format
The first line of the input file `editor.in` contains the number of commands $t$. Then follow the $t$ operations to perform.
To make the input file easier to read, newline characters may be inserted into the string of an `Insert` operation; please ignore them (if this is unclear, refer to the sample).
Except for newline characters, the ASCII codes of all characters in the input file lie in the closed interval $\left[32,126\right]$. There are no trailing spaces at the ends of lines.
We make the following assumptions:
- The number of `Move` operations does not exceed $5 \times 10^4$, the total number of `Insert` and `Delete` operations does not exceed $4 \times 10^3$, and the total number of `Prev` and `Next` operations does not exceed $2 \times 10^5$.
- The total number of characters inserted by all `INSERT` operations does not exceed $2$ MB ($1$ MB = $1024 \times 1024$ bytes), and the length of the correct output file does not exceed $3$ MB.
- When executing `Delete` and `Get`, there are always enough characters after the cursor. `Move`, `Prev`, and `Next` will never attempt to move the cursor to an invalid position.
- The input file contains no errors.
Hint for C++ contestants: As tested, on the largest testdata, using `fstream` for input may be about $1$ second slower than using `stdio`.
Output Format
Each line of the output file `editor.out` corresponds, in order, to the output of each `Get` command in the input file.
Explanation/Hint
:::info[About the sample output]
The first operation inserts `abcdefghijklmnopqrstuv wxy`, resulting in the text `abcdefghijklmnopqrstuv wxy`.
The third operation deletes $11$ characters after the $15$-th character, resulting in the text `abcdefghijklmno`.
The fifth operation inserts `^` after the $5$-th character, and the seventh operation inserts `_` after the $6$-th character, resulting in the text `abcde^_fghijklmno`.
The tenth operation inserts `.\/.` after the $8$-th character, resulting in the text `abcde^_f.\/.ghijklmno`.
The eleventh operation gets the $4$ characters after the $8$-th character and outputs `.\/.`.
The thirteenth operation inserts `^` after the $7$-th character, resulting in the text `abcde^_^f.\/.ghijklmno`.
The fifteenth operation gets the $22$ characters after the beginning and outputs `abcde^_^f.\/.ghijklmno`.
:::
Translated by ChatGPT 5