#K42412. Taco Text Editor with Undo/Redo
Taco Text Editor with Undo/Redo
Taco Text Editor with Undo/Redo
You are given a text editor that supports a limited set of operations. The editor starts with an empty document. You need to process a sequence of operations, and output the final state of the document.
The editor supports the following operations:
append(X)
: Appends the string X to the end of the document.delete(N)
: Deletes the last N characters from the document.undo()
: Reverts the last operation which has not already been undone.redo()
: Reapplies the last undone operation that has not been redone yet.
All operations are guaranteed to be valid. The arguments to append
and delete
operations are provided without additional quotes. For instance, append(hello)
means appending the string "hello".
Note that after performing an append or delete operation, any previously undone operations are cleared (i.e., you cannot redo them).
Mathematical Formulation:
If we denote the current document as \(D\), then:
- For
append(X)
: \(D \leftarrow D \, \Vert \; X\), where \(\Vert\) denotes string concatenation. - For
delete(N)
: \(D \leftarrow \text{prefix}(D, |D|-N)\).
inputFormat
The first line of the input contains a single integer \(n\) which indicates the number of operations to be performed. Each of the following \(n\) lines contains one operation. The operation will be one of the following four types: append(X)
, delete(N)
, undo()
or redo()
.
outputFormat
Output the final state of the document after processing all operations.
## sample1
append(hello)
hello