#K73412. Simplified Text Editor with Undo/Redo Operations

    ID: 33970 Type: Default 1000ms 256MiB

Simplified Text Editor with Undo/Redo Operations

Simplified Text Editor with Undo/Redo Operations

This problem involves implementing a simplified text editor which supports a series of operations. The editor starts with an empty text and supports the following commands:

  • APPEND x: Append the character x to the end of the text.
  • REMOVE: Remove the last character from the text (if the text is non-empty).
  • UNDO: Undo the most recent APPEND or REMOVE operation.
  • REDO: Redo the most recently undone operation (if any).

The operations should be applied sequentially. Note that after any new operation (APPEND or REMOVE), the redo history is cleared.

Example:

Consider the sequence of commands:
APPEND a
APPEND b
REMOVE
APPEND c
UNDO
REDO
REMOVE
The final text is a.

The task is to read a series of commands from standard input (stdin) and output the final text to standard output (stdout).

Mathematically, let \( T_0 = \varepsilon \) (empty string) and define the function \( f \) such that for each command \( c_i \), the updated text \( T_i = f(T_{i-1}, c_i) \). Operations like UNDO and REDO revert or reapply previous versions of \( T \) subject to their appropriate constraints.

inputFormat

The input is read from stdin and is formatted as follows:

  • The first line contains an integer \( n \) indicating the number of commands.
  • The next \( n \) lines each contain one command. Each command is one of the following forms:
    • APPEND x - where x is a single character.
    • REMOVE
    • UNDO
    • REDO

outputFormat

Output the final text after processing all commands. The output is printed to stdout as a single string.

## sample
7
APPEND a
APPEND b
REMOVE
APPEND c
UNDO
REDO
REMOVE
a