#P4567. [AHOI2006] 文本编辑器

[AHOI2006] 文本编辑器

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 kk-th character. If k=0k = 0, move the cursor to the position before the first character of the text.
  • Insert n (newline) S: Insert a string SS of length nn after the cursor, without moving the cursor. Here n1n \ge 1.
  • Delete n: Delete the nn characters after the cursor, without moving the cursor. Here n1n \ge 1.
  • Rotate n: Reverse the nn characters after the cursor, without moving the cursor. Here n1n \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 00 or more characters. Each character’s ASCII code is in the closed interval [32,126][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 NN. The following lines contain the NN operations to execute. Except for newline characters, all characters in the input file have ASCII codes in the closed interval [32,126][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.

10
Insert 13
Balanced eert
Move 2
Delete 5
Next
Insert 7
 editor
Move 0
Get
Move 11
Rotate 4
Get
B
t

Hint

For the input, we make the following assumptions:

  1. The number of MOVE operations does not exceed 5×1045 \times 10^4, the total number of INSERT, DELETE, and ROTATE operations does not exceed 6×1036 \times 10^3, the number of GET operations does not exceed 2×1042 \times 10^4, and the total number of PREV and NEXT operations does not exceed 2×1042 \times 10^4.
  2. The total number of characters inserted by all INSERT operations does not exceed 2M2M (where 1M=2201M = 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