The problem is to parse a series of commands to move the books that lie on the table. Initially there are n books lying on the table with book bi adjacent to book bi+1 for all 0 <= i < n-1, as shown in the diagram below:
|
Book N-1 |
|
...... |
|
Book 2 |
|
Book 1 |
|
Book 0 |
|
Table |
The valid commands and limited for moving books are:
Illegal commands:
All illegal commands should be ignored and should have no affect on the configuration of books.
Valid commands:
Puts book A on book B.
As below:
0 1 2 3 4
>> move 1 on 3
0 2 3 1 4
Puts book A under of book B.
As below:
0 1 2 3 4
>> move 1 under 3
0 2 1 3 4
Remove book A from the list.
As below:
0 1 2 3 4
>> remove 1
0 2 3 4
Reverse the entire list upside down.
As below:
0 1 2 3 4
>> reverse
4 3 2 1 0
Using A as pivot, exchange the stack above A (containing A) and the stack below A (not containing A).
As below:
0 1 2 3 4
>> switch 3
3 4 0 1 2
Finish moving the books, print the final status.
Remind you that solving this problem is relatively time-consuming.
However, 2 out of 3 testcases in this problem only require you to implement the original 4 commands (excluding reverse and switch A).
The input begins with an integer n on a line by itself representing the number of books in the book world. You may assume that 0 < n <= 10000.
The number of books is followed by a sequence of book commands, one command per line. Your program should process all commands until the exit command is encountered.
You may assume that all commands will be of the form specified above. There will be no syntactically incorrect commands.
Your output should contains one line of sequence which represents the order of books from the bottom to the top.
Each number is followed by a single space. And you are asked to add a new line character at the end.