#P4008. [NOI2003] 文本编辑器

    ID: 2939 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>字符串2003平衡树递归NOI 系列块状链表,块状数组,分块

[NOI2003] 文本编辑器

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 00 or more characters whose ASCII codes lie in the closed interval [32,126]\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
Move(k)\text{Move}(k) Move k Move the cursor to the position after the kk-th character; if k=0k = 0, move the cursor to the beginning of the text.
Insert(n,s)\text{Insert}(n,s) Insert n s Insert the string ss of length nn at the cursor. The cursor position does not change. Guarantee that n1n \geq 1.
Delete(n)\text{Delete}(n) Delete n Delete the nn characters after the cursor. The cursor position does not change. Guarantee that n1n \geq 1.
Get(n)\text{Get}(n) Get n Output the nn characters after the cursor. The cursor position does not change. Guarantee that n1n \geq 1.
Prev()\text{Prev}() Prev Move the cursor one character to the left.
Next()\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 tt. Then follow the tt 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 [32,126]\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×1045 \times 10^4, the total number of Insert and Delete operations does not exceed 4×1034 \times 10^3, and the total number of Prev and Next operations does not exceed 2×1052 \times 10^5.
  • The total number of characters inserted by all INSERT operations does not exceed 22 MB (11 MB = 1024×10241024 \times 1024 bytes), and the length of the correct output file does not exceed 33 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 11 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.

15
Insert 26
abcdefghijklmnop
qrstuv wxy
Move 15
Delete 11
Move 5
Insert 1
^
Next
Insert 1
_
Next
Next
Insert 4
.\/.
Get 4
Prev
Insert 1
^
Move 0
Get 22


.\/.
abcde^_^f.\/.ghijklmno

Hint

:::info[About the sample output] The first operation inserts abcdefghijklmnopqrstuv wxy, resulting in the text abcdefghijklmnopqrstuv wxy.

The third operation deletes 1111 characters after the 1515-th character, resulting in the text abcdefghijklmno.

The fifth operation inserts ^ after the 55-th character, and the seventh operation inserts _ after the 66-th character, resulting in the text abcde^_fghijklmno.

The tenth operation inserts .\/. after the 88-th character, resulting in the text abcde^_f.\/.ghijklmno.

The eleventh operation gets the 44 characters after the 88-th character and outputs .\/..

The thirteenth operation inserts ^ after the 77-th character, resulting in the text abcde^_^f.\/.ghijklmno.

The fifteenth operation gets the 2222 characters after the beginning and outputs abcde^_^f.\/.ghijklmno. :::

Translated by ChatGPT 5