Programming contests

Informatikai versenyfeladatok, gyakorló feladatsor, 2012. április 23.

April 23, 2012, 12:00 AM – May 21, 2012, 12:50 AM

ShellSort

He made each turtle stand on another one's back
And he 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 see 'most a mile!

King Yertle wishes to rearrange his turtle throne to place his highest-ranking nobles and closest advisors nearer to the top. A single operation is available to change the order of the turtles in the stack: a turtle can crawl out of its position in the stack and climb up over the other turtles to sit on the top.

Given an original ordering of a turtle stack and a required ordering for the same turtle stack, your job is to determine a minimal sequence of operations that rearranges the original stack into the required stack.

Input Specification

The first line of the input consists of a single integer K, giving the number of test cases. Each test case consists of an integer n, giving the number of turtles in the stack. The next n lines specify the original ordering of the turtle stack. Each of the lines contains 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 is a string of no more than eighty characters drawn from a character set consisting of the alphanumeric characters, the space character and the period (“.”). The next n lines in the input give the desired ordering of the stack, once again by naming turtles from top to bottom. Each test case consists of exactly 2n+1 lines in total. The number of turtles (n) will be less than or equal to two hundred.

Output Specification

For each test case, the output consists of a sequence of turtle names, one per line, indicating the order in which turtles are to leave their positions in the stack and crawl to the top. This sequence of operations should transform the original stack into the required stack and should be as short as possible. If more than one solution of shortest length is possible, any of the solutions may be reported. Print a blank line after each test case.

Sample Input

  1. 2
  2. 3
  3. Yertle
  4. Duke of Earl
  5. Sir Lancelot
  6. Duke of Earl
  7. Yertle
  8. Sir Lancelot
  9. 9
  10. Yertle
  11. Duke of Earl
  12. Sir Lancelot
  13. Elizabeth Windsor
  14. Michael Eisner
  15. Richard M. Nixon
  16. Mr. Rogers
  17. Ford Perfect
  18. Mack
  19. Yertle
  20. Richard M. Nixon
  21. Sir Lancelot
  22. Duke of Earl
  23. Elizabeth Windsor
  24. Michael Eisner
  25. Mr. Rogers
  26. Ford Perfect
  27. Mack
download as text file

Output for Sample Input

  1. Duke of Earl
  2. Sir Lancelot
  3. Richard M. Nixon
  4. Yertle
download as text file
University of Debrecen; Faculty of Informatics; v. 09/30/2024