Under construction

Still being built. Things may change

Text Editor

Time limit1sMemory limit150 MB

Summary
Implement a text editor over a string up to 2^63 characters with insert, erase, cut, paste, undo, redo, and a two-run serialize step.
Level

Hard9 of 10

Topics
Tree, Implementation, Simulation
Solved
No attempts yet

Problem

This is a run-twice problem: your solution is executed twice on each test. See the rest of the statement and the input format section for more details.

Your task is to implement a simple text editor that works with strings of characters with values in range 3333 to 126126 and implements the interface described below. The length of the string is guaranteed to be at most 263−12^{63} - 1 at all times. All parameters of the commands are 00-indexed and all intervals are semi-open: ℓ\ell is included while rr is excluded. All intervals are nonempty, and all positions and intervals are within bounds. The clipboard is initially empty.

  • insert pp str\mathit{str}: inserts string str\mathit{str} at the pp-th position of the file. The total length of all str\mathit{str} is at most 10610^6.
  • erase ℓ\ell rr: erases the contents of the file from the ℓ\ell-th to the rr-th character.
  • print ℓ\ell rr: prints the contents of the file from the ℓ\ell-th to the rr-th character. The total length of the output for all print commands does not exceed 10610^6.
  • copy ℓ\ell rr: copies the contents of the file from the ℓ\ell-th to the rr-th character to the clipboard.
  • cut ℓ\ell rr: cuts the contents of the file from the ℓ\ell-th to the rr-th character to the clipboard.
  • paste pp: inserts the contents of the clipboard at the pp-th position of the file. An empty clipboard is never pasted.
  • serialize: writes the contents of the file (but not the clipboard) to the output. The output must not exceed 10710^7 characters and may use only characters in the range 3333 to 126126. The output string does not have to represent the contents of the file exactly, but your program must be able to recreate the file in response to deserialize. You must print at least one character. This command is issued only once, as the last command of the first run.
  • deserialize str\mathit{str}: str\mathit{str} is exactly the string printed in response to serialize. This command is issued only once, as the first command of the second run.
  • undo: rolls back the changes made by the last command that changed the contents of the file (insert, erase, cut, paste, or deserialize), leaving the clipboard unchanged. If there is no previous version, the command does nothing.
  • redo: redoes the last undo that is not yet undone. If there is no next version, the command does nothing. As in most text editors, any command that changes the file also erases all next versions. Other commands, such as copy, do not.

Input

The first line contains a single integer nn (1≤n≤1041 \leq n \leq 10^4), the number of commands. The next nn lines contain the commands in the format described above. The input format is the same for both runs.

Output

Print the answers to all print and serialize commands, each on a separate line.

Examples2

  1. Example 1

    Input
    17
    insert 0 abcdef
    print 0 6
    erase 4 5
    print 0 5
    copy 0 3
    paste 1
    print 0 8
    cut 2 4
    print 0 6
    undo
    print 0 8
    paste 6
    print 0 10
    redo
    redo
    print 0 10
    serialize
    
    Expected output
    abcdef
    abcdf
    aabcbcdf
    aabcdf
    aabcbcdf
    aabcbcbcdf
    aabcbcbcdf
    serialize:aabcbcbcdf
    
  2. Example 2

    Input
    2
    deserialize serialize:aabcbcbcdf
    print 0 10
    
    Expected output
    aabcbcbcdf