Mirko received a birthday present from his aunt in the US ? a brand-new doubly-linked list (an example of which is shown in the figure below). The list contains N nodes numbered 1 through N.
Two types of moves can be done on the list:
Mirko played with his new toy for hours, writing down each move on a piece of paper so that he can reconstruct the list's initial state (nodes 1 through N in order from left to right).
When he decided to reconstruct the list, Mirko was astonished to find that there is no easy way to invert the moves and restore the list's initial state. Mirko cannot know where node X was prior to each move, only where it ended up.
Seeing how Mirko is still recovering from the shock, write a program that finds a minimal sequence of moves that restored the list's initial state from Mirko's logs.
Note: The sequence need not be unique.
input 2 1 A 2 1 output 1 A 1 2 input 4 3 B 1 2 A 4 3 B 1 4 output 2 A 1 2 B 4 3 input 6 5 A 1 4 B 2 5 B 4 2 B 6 3 A 3 5 output 3 A 4 5 B 6 5 A 2 3
출처:coci/2006-2007/contest3 6