Consulting
InterviewTime limit2sMemory limit1024 MB
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 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.