现在的位置: 首页 > 综合 > 正文

10152 – ShellSort

2018年01月12日 ⁄ 综合 ⁄ 共 2635字 ⁄ 字号 评论关闭

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 giving the number of test cases. Each test case consist on an integer giving the number of turtles in the stack. The next 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 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

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

题意:有n只乌龟,堆成一堆。按照从上到下的顺序输入乌龟的名字。每只乌龟都能够从自己的位置爬出来,然后爬到这堆乌龟的最上方。问最少需要多少次能够达到给定的顺序

按照先后顺序输出爬动的乌龟的名字

想法:一开始的时候想复杂了,想到乌龟可能爬了不止一次。后来考虑到既然是求最少的次数,那么乌龟最多只能爬一次。这样问题就简单了。

因为没用动的乌龟都会下沉,所以说没有动的乌龟的次序是不会发生改变的。用两个指针分别指向原先的顺序,和要排序的顺序。

然后对应的乌龟如果名字相同,则指针分别向上走。如果不同,则指向原先的指针向上走。

具体实现看代码。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
using namespace std;
int main ()
{
    int t,n,i,j;
    cin>>t;
    while(t--)
    {
        cin>>n;
        getchar();
        char name[210][100];
        char sort[210][100];
        for (i=0; i<n; i++)
            gets(name[i]);
        for (i=0; i<n; i++)
            gets(sort[i]);
        i=j=n-1;
        while(i>=0 && j>=0)
        {
            if (strcmp(name[i],sort[j])==0)
            {
                i--;
                j--;
            }
            else i--;
        }
        for (i=j; i>=0; i--)
            puts(sort[i]);
        cout<<endl;
    }
    return 0;
}

抱歉!评论已关闭.