#C3514. Simulating Undo in a Text Editor
Simulating Undo in a Text Editor
Simulating Undo in a Text Editor
You are given a series of operations to simulate a simple text editor with undo functionality. The editor starts with an empty text. It supports the following operations:
- add <char>: Append the character char to the end of the current text.
- delete: Remove the last character from the text (if any).
- undo: Undo the most recent add or delete operation that has not yet been undone.
The undo operation reverses the effect of the last non-undone operation. For an add operation, it removes the appended character; for a delete operation, it restores the deleted character. Formally, if the last operation was an add
of character c, then undo removes that c; if it was a delete
of character c, then undo appends c back to the text.
In LaTeX, the undo effect of an operation can be described as follows:
\( \text{undo}( \text{add}(c)) = \text{delete}(c) \) and \( \text{undo}( \text{delete}(c)) = \text{add}(c) \).
Your task is to simulate these operations and output the final text after processing all operations.
inputFormat
The input is read from standard input (stdin) and is structured as follows:
- The first line contains an integer
n
representing the number of operations. - The next
n
lines each contain a string representing an operation. Each operation is one of the following forms:add <char>
,delete
, orundo
.
outputFormat
Output to standard output (stdout) a single line that is the final text after all operations are processed.
## sample6
add a
add b
add c
delete
undo
undo
ab