10152-shellsort
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_ problem&problem=1093
He made each turtle stand on another one's back and his piled them all up in a nine-turtle stack. And then Yertle climbed up. He sat down on the pile. What a wonderful view! He could ' most a mile!
The Problem
King Yertle wishes to rearrange he turtle throne to place he highest-ranking nobles and closest advisors to the T Op. A single operation are available to change the order of the Turtles in the Stack:a turtle can crawl out of their position in The stack and climb up over the "other turtles" to sit on the top.
Given A original ordering of a turtle stack and a required for the ordering same stack, turtle job are to your e A minimal sequence of operations that rearranges the original stack into the required stack.
The ' the ' input consists of a single integer K giving the number of test cases. Each test case consist on a integerngiving the number of turtles in the stack. The Nextn lines specify the original ordering of the turtle stack. Each of the lines contains is the name of a turtle, starting with the "turtle on the" top of the "stack" and working down to the Turtle at the bottom of the stack. Turtles have unique names, each of which are a string of no more than eighty characters drawn from a character set Consisti Ng of the alphanumeric characters, the space character and the period ('. '). The nextnlines in the input gives the desired ordering of the stack, once again by naming turtles. Each test case consists of exactly 2n+1 lines. The number of turtles (n) is less than or equal to two hundred.
For the all test case, the output consists of a sequence of turtle names, one/line, indicating the order in which turtles are to leave their positions in the stack and crawl to the top. This is sequence of operations should transform the original stack into the required stack and should is as short as possible . If more than one solution of shortest length are possible, any of the solutions may reported. Print a blank line after the each test case.
Sample Input
2
3
yertle
Duke of Earl
Sir Lancelot
Duke of Earl
Yertle
Sir Lancelot
9
Yertle
Duke of Earl
Sir Lancelot
Elizabeth Windsor
Michael Eisner
Richard m. Nixon
Mr Rogers
Ford Perfect
Mack
yertle
Richard M. Nixon
Sir Lancelot
Duke of Earl
Elizabeth Windsor
Michael Eisner
Mr Rogers
. Ford Perfect
Mack
Sample Output
Duke of Earl
Sir Lancelot
Richard M. Nixon
Yertle
Idea: First find the string that needs to move, the method is as follows: Take the initial sequence as quasi, set the initial sequence subscript I, the purpose sequence is labeled J, starting from n-1, if the corresponding string of two subscript equal, subscript minus one, otherwise only the initial sequence subscript minus one. The string that has not been successfully matched in the destination sequence is the string that needs to be moved. To minimize the number of moves, it is obvious that you should move the string in reverse order (output) in an unhandled destination sequence.
The idea comes from: http://www.cppblog.com/wuxu/archive/2011/11/22/160719.aspx