Uva 10152 ShellSort

来源:互联网 发布:淘宝店网页制作 编辑:程序博客网 时间:2024/05/21 07:46
ShellSort

Description

Download as PDF

Problem D: 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!

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 integer K giving the number of test cases. Each test case consist on an integern 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 nextn lines 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 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

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

题目大意:

有一堆乌龟, 从上到下叠好,各自有自己的名字。然后题目给出两个序列, 第一个是初始序列,第二个是目标序列, 题目要求是以最少的步骤把初始序列转换成目标序列,转换的操作为:每次只能选中一只乌龟,把这个乌龟抽出来放到最顶端,其它的乌龟则全部降落一格。  

解题思路:

要使步骤最少,那么要保证相对顺序是符合目标序列的不要进行移动(即最大公共子序列不要进行移动)。

可以设置两个坐标指向两个数组的最后一个,然后往前枚举,碰到相同的,则两个坐标都减1, 如果不相同的,则初始数组的坐标减一。

最后指向初始数组的坐标移动到了尽头,会发现目标数组的坐标还没到尽头,那么在这个坐标之前的都是不符合顺序的,就要移动这些乌龟。

然后剩下的这些乌龟, 按目标数组的逆序输出即可。


#include <iostream>#include <stdio.h>#include <string>#include <stdio.h>using namespace std;const int N = 201;string str1[N],str2[N];int main() {int t,n;while(cin >> t) {while( t-- ) {cin >> n; getchar();for(int i = 0;i < n; i++) {getline(cin,str1[i]);}for(int i = 0;i < n; i++) {getline(cin,str2[i]);}int pos = 0;for(int i=n-1,j=n-1; j >= 0 and i>=0;) {if(str2[i] == str1[j]) {i--;j--;pos = i;}elsej--;}for(int i=pos;i >= 0;i--) {cout<<str2[i]<<endl;}cout<<endl;}}return 0;}

0 0