#K91622. Text Editor with Undo Operation
Text Editor with Undo Operation
Text Editor with Undo Operation
You are required to implement a simple text editor that supports four operations: Type, Delete, Get, and Undo.
The editor maintains a text (which is initially empty) and a history of operations. The operations are defined as follows:
- Type x: Append character \( x \) to the text.
- Delete: Delete the last character from the text (if the text is not empty).
- Get k: Output the \( k \)-th character in the text (1-indexed). If \( k \) is out of range, output an empty string.
- Undo: Undo the last Type or Delete operation (if any).
You should process a sequence of operations, and for every Get
operation, output the result on a new line. All inputs are given via stdin
and outputs should be written to stdout
.
Note: The undo operation only reverts Type or Delete operations and does nothing if there is no operation to undo.
inputFormat
The first line of the input contains an integer \( n \) denoting the number of operations.
The following \( n \) lines each contain one operation in one of the formats:
Type x
Delete
Get k
Undo
outputFormat
For each Get
operation, output the corresponding character on a new line. If the request is out of bounds, output an empty line.
8
Type a
Type b
Type c
Get 3
Delete
Get 2
Undo
Get 3
c
b
c
</p>