#C3725. Simple Text Editor with Undo
Simple Text Editor with Undo
Simple Text Editor with Undo
You are given a text editor that starts with an empty string. You will be provided a number of operations to perform on the string. Each operation is either adding a character to the end of the string or undoing the most recent add operation. Formally, the operations can be described as follows:
For each operation:
- If the operation is of the form
ADD x
(wherex
is a single character), appendx
to the end of the current string. - If the operation is
UNDO
, remove the last added character from the current string if there is any.
Mathematically, if we denote the string after performing i operations as \( S_i \), then for an operation ADD x
, we have \( S_i = S_{i-1} + x \); for UNDO
, if \( S_{i-1} \) is non-empty, then \( S_i \) is \( S_{i-1} \) with its last character removed.
Your task is to compute the final state of the string after all operations have been executed.
inputFormat
The input is given via stdin:
- The first line contains a single integer
n
(1 ≤ n ≤ 10^5), representing the number of operations. - The following
n
lines each contain an operation, either in the formADD x
wherex
is a single character, orUNDO
.
outputFormat
Output the final string after performing all the operations. The output should be written to stdout as a single line.
## sample5
ADD a
ADD b
UNDO
ADD c
ADD d
acd
</p>