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

uva 10152 – ShellSort

2013年08月09日 ⁄ 综合 ⁄ 共 2868字 ⁄ 字号 评论关闭
文章目录

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 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 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

给定初始序列,根据移动规则用最少次数移动到目标序列,规则是只能将任意数据移动到顶部。

为了移动步数最少,所以初始序列中已经有序的就不必移动,然后每次移动一个无序的到有序位置;

下图是从别人那直接copy的

Yertle                1
Duke of Earl          2
Sir Lancelot          3
Elizabeth Windsor    4
Michael Eisner        5
Richard M. Nixon      6
Mr. Rogers            7
Ford Perfect          8
Mack                  9

table_4:

Yertle                1
Richard M. Nixon      6
Sir Lancelot          3
Duke of Earl          2
Elizabeth Windsor    4
Michael Eisner        5
Mr. Rogers            7
Ford Perfect          8
Mack                  9

所谓的已经有序的序列就是,目标序列底部开始的连续递减序列的最大长度。

自己想了很久看了别人的,发现这个方法,还是很巧妙地就是证明为什么是最小的真不知道╮(╯▽╰)╭

#include <stdio.h>
#include <string.h>
struct node
{char s[200];
}s1[300],s2[300];
void main()
{int n,i,j,t,num[300];
 scanf("%d\n",&t);
 while (t--)
 {
 scanf("%d\n",&n);
 for (i=1;i<=n;i++)
 gets(s1[i].s);
 for (i=1;i<=n;i++)
 gets(s2[i].s);
 for (i=1;i<=n;i++)
 {
  for (j=1;j<=n;j++)
   if (strcmp(s2[i].s,s1[j].s)==0)
   {num[i]=j; break;}
 }
 while (num[n]>num[n-1]) --n;
 for (i=n-1;i>=1;i--)
 puts(s1[num[i]].s);
 printf("\n");
 }
}

抱歉!评论已关闭.