P4567 [AHOI2006] Text Editor

Description

These days, Keke is not playing with Kaka, because Keke is obsessed with making a simple and efficient text editor. Can you help him? To clarify the goal, Keke gives an abstract definition of a "text editor": - `Move k`: Move the cursor to the position after the $k$-th character. If $k = 0$, move the cursor to the position before the first character of the text. - `Insert n (newline) S`: Insert a string $S$ of length $n$ after the cursor, without moving the cursor. Here $n \ge 1$. - `Delete n`: Delete the $n$ characters after the cursor, without moving the cursor. Here $n \ge 1$. - `Rotate n`: Reverse the $n$ characters after the cursor, without moving the cursor. Here $n \ge 1$. - `Get`: Output the one character after the cursor, without moving the cursor. - `Prev`: Move the cursor one character to the left. - `Next`: Move the cursor one character to the right. Definitions: - Text: A sequence of $0$ or more characters. Each character’s ASCII code is in the closed interval $[32, 126]$ or is a newline character. That is, characters are all visible characters or spaces, or newlines. - Cursor: A marker indicating a position in a text. It can be before the first character, after the last character, or between two adjacent characters. - Text editor: A program that performs the above seven operations on a piece of text and a cursor within it. If the text is empty, we say the text editor is empty. Write a program to: 1. Create an empty text editor. 2. Read a sequence of operation commands from the input file and execute them. 3. For every executed `Get` operation, write the specified content to the output file.

Input Format

The first line of the input file contains the number of commands $N$. The following lines contain the $N$ operations to execute. Except for newline characters, all characters in the input file have ASCII codes in the closed interval $[32, 126]$. There are no trailing spaces at the end of lines.

Output Format

Output the results corresponding to each `Get` command in the input file, in order, with no extra characters. Note: If the output of a `Get` command is a newline character, no additional newline is needed.

Explanation/Hint

For the input, we make the following assumptions: 1. The number of `MOVE` operations does not exceed $5 \times 10^4$, the total number of `INSERT`, `DELETE`, and `ROTATE` operations does not exceed $6 \times 10^3$, the number of `GET` operations does not exceed $2 \times 10^4$, and the total number of `PREV` and `NEXT` operations does not exceed $2 \times 10^4$. 2. The total number of characters inserted by all `INSERT` operations does not exceed $2M$ (where $1M = 2^{20}$). 3. When executing `DELETE`, `ROTATE`, and `GET`, there are always enough characters after the cursor. `MOVE`, `PREV`, and `NEXT` will not move the cursor to an invalid position. 4. The input file has no errors. Translated by ChatGPT 5