#P4008. [NOI2003] 文本编辑器
[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 or more characters whose ASCII codes lie in the closed interval .
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 | Move the cursor to the position after the -th character; if , move the cursor to the beginning of the text. | |
| Insert n s | Insert the string of length at the cursor. The cursor position does not change. Guarantee that . | |
| Delete n | Delete the characters after the cursor. The cursor position does not change. Guarantee that . | |
| Get n | Output the characters after the cursor. The cursor position does not change. Guarantee that . | |
| Prev | Move the cursor one character to the left. | |
| 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
GEToperation, write the specified content to the output file.
Input Format
The first line of the input file editor.in contains the number of commands . Then follow the 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 . There are no trailing spaces at the ends of lines.
We make the following assumptions:
- The number of
Moveoperations does not exceed , the total number ofInsertandDeleteoperations does not exceed , and the total number ofPrevandNextoperations does not exceed . - The total number of characters inserted by all
INSERToperations does not exceed MB ( MB = bytes), and the length of the correct output file does not exceed MB. - When executing
DeleteandGet, there are always enough characters after the cursor.Move,Prev, andNextwill 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 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 characters after the -th character, resulting in the text abcdefghijklmno.
The fifth operation inserts ^ after the -th character, and the seventh operation inserts _ after the -th character, resulting in the text abcde^_fghijklmno.
The tenth operation inserts .\/. after the -th character, resulting in the text abcde^_f.\/.ghijklmno.
The eleventh operation gets the characters after the -th character and outputs .\/..
The thirteenth operation inserts ^ after the -th character, resulting in the text abcde^_^f.\/.ghijklmno.
The fifteenth operation gets the characters after the beginning and outputs abcde^_^f.\/.ghijklmno.
:::
Translated by ChatGPT 5
京公网安备 11011102002149号