← Back to problems

9. Text Editor with Undo/Redo

HARD
DESIGNLLDStackUndo Redo

Implement a single-document text editor supporting insert, delete, replace, and a standard undo/redo stack where any new edit clears the redo history.

Implement a text editor over a single string document, indexed from 0. Process commands from stdin, one per line. Editing operations (each counts as exactly ONE undoable unit): - INSERT <pos> <text> : insert <text> so its first character lands at index <pos>. Valid pos is 0..len (len = append at end). - DELETE <pos> <count> : delete <count> characters starting at index <pos>. The range [pos, pos+count) must lie within the document. - REPLACE <pos> <count> <text> : remove <count> characters starting at <pos>, then insert <text> at <pos>, as a single undoable unit. Control operations: - UNDO : revert the most recently applied edit. If there is nothing to undo, print "NOP". - REDO : reapply the most recently undone edit. If there is nothing to redo, print "NOP". - PRINT : output the current document, or "<empty>" if it is empty. Rules: - Undo/redo behave as two stacks. UNDO moves the last applied edit onto the redo stack; REDO moves it back. - Performing ANY new editing operation (INSERT, DELETE, or REPLACE) clears the redo stack : the previously undone edits can no longer be redone. - If an editing operation's position/range is invalid (out of bounds), print "ERR" and leave the document and both stacks completely unchanged. - <text> tokens contain no spaces.
Log in to submit a solution

Comments

Log into join the discussion.