#C13900. Text Editor with Undo and Error Handling

    ID: 43490 Type: Default 1000ms 256MiB

Text Editor with Undo and Error Handling

Text Editor with Undo and Error Handling

Implement a simple text editor that supports three operations: ADD, DELETE, and UNDO. Initially, the document is empty. The commands operate as follows:

  • ADD pos text: Insert the given text at the specified position in the document. The position must satisfy \(0 \le pos \le |document|\).
  • DELETE pos len: Delete exactly len characters starting from position pos. It is required that \(0 \le pos\) and \(pos + len \le |document|\).
  • UNDO: Revert the most recent ADD or DELETE operation.

If a command attempts to access an invalid position, the program should output an error message and exit immediately. The error messages are:

  • Position out of range for an invalid ADD operation.
  • Position or length out of range for an invalid DELETE operation.

inputFormat

The first line contains an integer T denoting the number of operations.

The following T lines each contain one command. Each command is in one of the following forms:

  • ADD pos text — Insert text at position pos. Note that text may contain spaces.
  • DELETE pos len — Delete len characters starting from position pos.
  • UNDO — Revert the most recent operation.

outputFormat

Print the final state of the document to standard output. If an invalid operation is encountered, print the corresponding error message and exit immediately.

## sample
4
ADD 0 Hello
ADD 5  World
UNDO
ADD 5 , World
Hello, World