#K90692. Simulate a Text Editor
Simulate a Text Editor
Simulate a Text Editor
You are given a simple text editor that supports the following operations:
INSERT <word>
: Append the given word to the end of the current text.DELETE <n>
: Delete the last n characters from the current text. If n is more than the current text length, delete as many as possible (resulting in an empty text).UNDO
: Undo the most recent text modification operation (i.e. an INSERT or DELETE). If there is no operation to undo, do nothing.PRINT
: Output the current state of the text.
Initially, the text editor is empty. Your task is to simulate the operations and produce the outputs for all PRINT
operations.
Note: Every modification (INSERT or DELETE) must be saved in a history to allow the undo operation to revert to the previous state.
The formulas involved in the simulation are straightforward. For example, if the current text is \(T\) and an operation DELETE \(n\) is issued, the new text becomes \(T[0 : |T| - n]\) (using 0-indexed notation), where \(|T|\) is the length of \(T\).
inputFormat
The input is given through standard input in the following format:
N operation_1 operation_2 ... operation_N
Where:
N
is an integer denoting the total number of operations.- Each subsequent line represents a single operation, formatted as one of:
INSERT <word>
DELETE <n>
UNDO
PRINT
outputFormat
For each PRINT
operation in the input, output the current state of the text on a single line, using standard output.
9
INSERT hello
INSERT world
PRINT
DELETE 5
PRINT
UNDO
PRINT
UNDO
PRINT
helloworld
hello
helloworld
hello
</p>