Under construction

Still being built. Things may change

Consulting

Interview

Time limit2sMemory limit1024 MB

Summary
Given WRITE and READ commands on named memories, insert the fewest WAIT lines so that no two conflicting commands run at the same time.
Level

Medium5 of 10

Topics
Greedy, Hash map, Implementation
Solved
No attempts yet

Problem

The Sogang ICPC Team founded the Sogang Program Consulting Team (SPC Team) to help its members. The SPC Team started offering free debugging consultations for code with errors, hoping to build friendly ties with the members.

One day, a program puzzled the whole SPC Team. It looked like normal code, but it did not produce the data the member wanted. After more debugging, the SPC Team found that several lines of commands were running at the same time. Look at this example.

1: WRITE A TO B
2: WRITE B TO C
3: READ B
4: READ C
5: EXIT

If the commands run in order, the code has no problem. But if lines 1 to 4 run at the same time, problems appear. If line 2 runs before any data has moved from memory A to memory B, the data that ends up in memory C is unknown. The SPC Team fixed the code like this.

1: WRITE A TO B
2: WAIT
3: WRITE B TO C
4: WAIT
5: READ B
6: READ C
7: EXIT

With a WAIT between the commands, WRITE A TO B and WRITE B TO C no longer run at the same time, so the data in memory C is clear. The SPC Team sorted the cases that can cause problems into three types.

  • READ with WRITE
    • If WRITE A TO B and READ B run at the same time, the data in memory B is uncertain, so a WAIT must be placed between the two commands.
    • If WRITE A TO B and WRITE B TO C run at the same time, the data in memory C is uncertain, so a WAIT must be placed between the two commands.
  • WRITE with WRITE
    • If WRITE A TO C and WRITE B TO C run at the same time, the data in memory C is uncertain, so a WAIT must be placed between the two commands.
    • Two WRITE A TO B commands can run at the same time without a wrong result, but for program stability a WAIT must be placed between them.
  • Deadlock
    • If WRITE A TO B and WRITE B TO A run at the same time, the values in memory A and memory B are uncertain, so a WAIT must be placed between the two commands.

Members keep sending consultation requests. Tired of the repeated work, the SPC Team wants a program that detects these situations and gives the consultation automatically. The SPC Team is too busy to build it, so it asked you, a capable programmer, to write the program.

Input

Up to 10 00010\ 000 lines of commands are given. Each line is a WRITE, READ, or EXIT command. The EXIT command appears only once, at the end.

Each command is defined as follows. A memory name consists of 1 to 3 uppercase English letters.

  • WRITE A TO B: Moves the contents of memory A to memory B. After this, memory A is in the READ state.
  • READ A: Reads the data in memory A.
  • EXIT: Ends the program.

No input has a WRITE that uses the same memory on both sides, such as WRITE A TO A.

Output

Print the consultation result that uses the fewest WAIT commands, keeping the original order of the commands.

Print one command per line. If more than one consultation result is possible, print any one of them.

Examples2

  1. Example 1

    Input
    WRITE A TO B
    WRITE B TO C
    READ B
    READ C
    EXIT
    
    Expected output
    WRITE A TO B
    WAIT
    WRITE B TO C
    WAIT
    READ B
    READ C
    EXIT
    
  2. Example 2

    Input
    WRITE EAX TO ECX
    WRITE ESP TO ESI
    READ EAX
    WRITE EDX TO EBP
    WRITE EDI TO ESI
    READ ECX
    WRITE ECX TO EAX
    READ EBP
    WRITE ESP TO EBP
    WRITE EBP TO EDX
    EXIT
    
    Expected output
    WRITE EAX TO ECX
    WRITE ESP TO ESI
    READ EAX
    WRITE EDX TO EBP
    WAIT
    WRITE EDI TO ESI
    READ ECX
    WRITE ECX TO EAX
    READ EBP
    WAIT
    WRITE ESP TO EBP
    WAIT
    WRITE EBP TO EDX
    EXIT