Account
Draft saved on this device
▤ DescriptionOriginal CppValley problem
Design PatternsEasy

Create A State Machine For A Door Lock

Model a door with three states: CLOSED_UNLOCKED (initial), CLOSED_LOCKED, and OPEN.

Model a door with three states: CLOSED_UNLOCKED (initial), CLOSED_LOCKED, and OPEN. LOCK is valid only when CLOSED_UNLOCKED; UNLOCK only when CLOSED_LOCKED; OPEN only when CLOSED_UNLOCKED; CLOSE only when OPEN. Invalid commands leave the state unchanged and print ERROR. Valid commands print the resulting state.

Input format

First line Q; next Q lines LOCK, UNLOCK, OPEN or CLOSE.

Output format

Print ERROR for invalid commands; otherwise print CLOSED_UNLOCKED, CLOSED_LOCKED or OPEN.

Examples

Example 1
Input
5
LOCK
OPEN
UNLOCK
OPEN
CLOSE
Expected output
CLOSED_LOCKED
ERROR
CLOSED_UNLOCKED
OPEN
CLOSED_UNLOCKED

Constraints

  • 0 ≤ Q ≤ 100000

Related concepts

State Pattern

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
5
LOCK
OPEN
UNLOCK
OPEN
CLOSE
Expected output
CLOSED_LOCKED
ERROR
CLOSED_UNLOCKED
OPEN
CLOSED_UNLOCKED
Execution requires a secure external judge.