Text Editor Undo/Redo System
You are implementing a simple text editor that supports basic operations and undo/redo functionality. The editor maintains a document (initially empty) and must support the following operations:
- INSERT
pos char: Insert charactercharat positionpos(0-indexed). Characters to the right shift one position. Ifposis out of bounds, do nothing. - DELETE
pos: Delete the character at positionpos(0-indexed). Characters to the right shift one position left. Ifposis out of bounds or the document is empty, do nothing. - UNDO: Undo the most recent operation that modified the document (INSERT or DELETE). If there is nothing to undo, do nothing.
- REDO: Redo the most recently undone operation. If there is nothing to redo, do nothing.
- QUERY: Output the current state of the document.
The key constraint: when you perform any INSERT or DELETE, the redo history is cleared (as in real text editors).
Input
The first line contains n (1 ≤ n ≤ 1000), the number of operations.
Each of the next n lines contains one operation:
INSERT pos charwhere0 ≤ pos ≤ lengthandcharis a lowercase letterDELETE poswherepos ≥ 0UNDOREDOQUERY
Output
For each QUERY operation, print the current document on a single line. If the document is empty, print an empty line.
Notes
- The document starts empty.
- When you INSERT or DELETE, the redo stack is cleared because the history branches.
- Undo/redo are a no-op if there is nothing to undo/redo.