#K16046. Text Editor Simulation
Text Editor Simulation
Text Editor Simulation
You are asked to implement a simulation of a simple text editor that processes a sequence of operations. The editor supports four types of operations:
- 1 x: Insert character x at the end of the current text.
- 2: Delete the last character of the current text.
- 3 k: Print the \(k\)-th character of the current text (1-indexed).
- 4: Undo the last operation (either an insert or a delete).
The commands will be given one per line. When the print command (3 k) is executed, the result should be output immediately (or collected and printed after processing all operations), with each printed character on a new line.
Note: The undo operation reverses the effect of the last non-undone insert or delete operation. In particular, if the last operation was:
- an insert, then undoing will remove the last character.
- a delete, then undoing will reinsert the removed character at the end of the text.
Be careful with the indices: for a print operation the index \(k\) is 1-indexed, i.e., the first character is at position 1.
inputFormat
The first line contains a single integer \(n\) representing the number of operations.
The following \(n\) lines each contain an operation in one of the following formats:
1 x
- Insert character \(x\).2
- Delete the last character.3 k
- Print the \(k\)-th character.4
- Undo the last operation.
It is guaranteed that the operations are valid.
outputFormat
For each print operation (command 3), output the corresponding character on a new line. If there are no print operations, output nothing.
## sample8
1 a
1 b
1 c
3 2
2
3 2
4
3 2
b
b
b
</p>