The question of the WordPress 10152-ShellSort
Sort by his method, move one to the vertex each time, and arrange the sequence as needed.
Problem D: ShellSortHe 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 coshould see 'most a mile! The Problem
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.
The first line of the input consists of a single integerKGiving the number of test cases. Each test case consist on an integerNGiving the number of turtles in the stack. The nextNLines 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 nextNLines in the input gives 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.
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 shocould transform the original stack into the required stack and shocould 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
23YertleDuke of EarlSir LancelotDuke of EarlYertleSir Lancelot9YertleDuke of EarlSir LancelotElizabeth WindsorMichael EisnerRichard M. NixonMr. RogersFord PerfectMackYertleRichard M. NixonSir LancelotDuke of EarlElizabeth WindsorMichael EisnerMr. RogersFord PerfectMack
Sample Output
Duke of EarlSir LancelotRichard M. NixonYertle
#include
#include
#include
#include
using namespace std;int main(){int T, n;string s;cin>>T;while (T--){cin>>n;getchar();vector
vstr;for (int i = 0; i < n; i++){getline(cin, s);vstr.push_back(s);}vector
order;for (int i = 0; i < n; i++){getline(cin, s);order.push_back(s);}int j = n-1;for (int i = n-1; i >= 0; i--){if (order[j] == vstr[i]) j--;}while (j >= 0) puts(order[j--].c_str());putchar('\n');}return 0;}