Account
← Problems/Design Patterns
Draft saved on this device
▤ DescriptionOriginal CppValley problem
Design PatternsEasy

Implement A Command History With Undo

A text editor stores a single integer document value initially 0.

A text editor stores a single integer document value initially 0. SET x sets the document to x and pushes the old value on undo history. UNDO restores the previous value if there is history; otherwise does nothing. PRINT emits the current document value.

Input format

First line Q; next Q lines SET x, UNDO or PRINT.

Output format

Print one value for every PRINT.

Examples

Example 1
Input
7
SET 4
SET 9
PRINT
UNDO
PRINT
UNDO
PRINT
Expected output
9
4
0

Constraints

  • 0 ≤ Q ≤ 100000
  • -10^9 ≤ x ≤ 10^9

Related concepts

Command History

This early-access exercise uses standard input/output. More advanced concurrency challenges will use specialized class interfaces, instrumentation, and stress tests once the judging infrastructure is ready.

⌘ CodeC++17
GNU C++17 · main.cpp

Loading Monaco C++ editor…

Checking judge...

Checking whether the external C++ judge is available.

⌁ Test cases & resultsPublic samples: 1
Input
7
SET 4
SET 9
PRINT
UNDO
PRINT
UNDO
PRINT
Expected output
9
4
0
Execution requires a secure external judge.