National Olympiad in Informatics, China, 2003
A long, long time ago, DOS3.x programmers were starting to get tired of EDLIN. So they gradually switched to editors that they wrote themselves.
Many years later, Alex has accidentally stumbled upon a text editor from back in these days. After some basic testing, he has made a shocking discovery: this software is able to perform tens of thousands of edit operations per second (of course, you cannot conduct these tests by hand)! So, Alex is spending sleepless nights trying to create a product like this himself. Can you help him?
To clarify the objective, Alex has created some definitions to make the term "text editor" less abstract:
- text: a sequence formed using 0 or more ASCII characters with values in the inclusive range (i.e. spaces and visible characters).
- cursor: a mark within a block of text to indicate a position. It may be positioned at the beginning of the text, the end of the text, or between any two adjacent characters in the text.
- text editor: a program containing a block of text and a single cursor, capable of performing the following six operations. If the block of text is empty, we will consider the text editor to be empty.
Operation | Input Format | Function |
---|---|---|
Move() | Move k |
Moves the cursor's position to immediately after the -th character in the text. If , then the cursor is moved to the start of the text. |
Insert(, ) | Insert n↵ s |
Inserts a string of length () at the position of the cursor. The position of the cursor does not change. |
Delete() | Delete n |
Delete the () characters following the cursor. The position of the cursor does not change. |
Get() | Get n |
Outputs the () characters following the cursor. The position of the cursor does not change. |
Prev | Prev |
Moves the cursor backwards by one character. |
Next | Next |
Moves the cursor forwards by one character. |
For example, an empty text editor when given the operations:
Insert(13, Balanced tree
), Move(2), Delete(5), Next(),
Insert(7, editor
), Move(0), and Get(16), will output Bad editor tree
.
Your task is to:
- Create an empty text editor.
- Read some operations from the input and execute them.
- For each Get() operation, output the correct contents.
Input Specification
The first line of input contains an integer , the number of
operations. The following lines contain operations.
To make text from the text editor easier to read, the string following
the Insert() operation may contain multiple line breaks. You must ignore
them (if this is hard to understand, please refer to the sample input
and output below).
Other than newline characters, all of the characters in the input will
have ASCII values within the range , inclusive. There will be
no trailing spaces.
You can assume that for each test case:
- The number of Move() operations will not exceed . The total number of Insert() and Delete() operations will not exceed . The total number of Prev() and Next() operations will not exceed .
- The number of characters inserted in each Insert() operation will not exceed 2M (1M ). The length of the correct output will not exceed 3M.
- For each Delete() and Get() operation, there will always be sufficient characters following the cursor. The Move(), Prev(), and Next() operations will never move the cursor to an invalid position.
- All operations will be valid.
Output Specification
Each line of the output should contain the text outputted by a single corresponding Get() operation.
Sample Input
15
Insert 26
abcdefghijklmnop
qrstuv wxy
Move 16
Delete 10
Move 5
Insert 1
^
Next
Insert 1
_
Next
Next
Insert 4
.\/.
Get 4
Prev
Insert 1
^
Move 0
Get 22
Sample Output
.\/.
abcde^_^f.\/.ghijklmno
Problem translated to English by .
Comments