#K37422. Text Editor Simulation
Text Editor Simulation
Text Editor Simulation
You are to implement a text editor that supports four operations:
APPEND <string>
: Append the given string to the end of the current text.DELETE <k>
: Delete the last \(k\) characters from the current text.PRINT <k>
: Print the \(k\)th character of the current text (1-indexed). If the position is invalid, output an empty line.UNDO
: Undo the most recent APPEND or DELETE operation, restoring the text to its previous state.
The operations are executed sequentially. The text editor starts with an empty string. Make sure your solution correctly handles undo operations and edge cases such as printing from an empty string or deleting more characters than exist.
inputFormat
The first line contains an integer \(Q\) (the number of operations). Each of the next \(Q\) lines contains a command in one of the following forms:
APPEND <string>
DELETE <k>
PRINT <k>
UNDO
All input is given through standard input (stdin).
outputFormat
For each PRINT
operation, print the result (a single character or an empty line if the position is invalid). Output each result on its own line through standard output (stdout).
2
APPEND hello
PRINT 1
h
</p>