UVa 10152 - ShellSort

本文深入探讨了UVa10152 ShellSort问题的解决方案,包括问题背景、核心思路、代码实现及参考资源。通过详细分析,读者能够掌握如何高效地对堆栈中的乌龟进行重新排列,以达到指定的顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UVa 10152 - ShellSort

1题目

===================

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

===================

2思路

关键是要理清楚操作的过程,精髓只有一句话,就是不要想操作的具体过程,直接看最后 结果。假如要从14235移动成12345,其实就是把123抽出来,之后45自然成序,并且123 的移动次序也是确定的,由于抽取后必须放在头部,所以移动次序必须先3再2再1, 这样才能保证最后的顺序是123。

我的思路是先找出不用移动的,就是把14235从后向前遍历,找到54这个递减序列作为不动 的序列。注意必须是从最大的5开始,因为这些序列今后就不动了,他们要垫底。你如果让 5321不动,那4最后无论如何也到不了正确位置。所以先找5,再找4…,直到到达14235开头。 这时候12345中45被标记为不动,只要依次输出321就行了。123作为需要动的序列,输出次序 与他们的正确顺序相反。

再举个例子,由143562789到123456789,先找不动序列,98765,再输出4321就行了。因为 98765是最大不动序列,那么4321就是最小移动序列。

3代码

/*
 * Problem: UVa 10152 - ShellSort
 * Lang: ANSI C
 * Time: 0.046
 * Author: minix
 */
#include <stdio.h>
#include <string.h>

#define N (200+10)
#define M (80+10)
char s[N][M];
char d[N][M];
int flag[N];

int main () {
  int count;
  int i, j, k;
  int n;

  scanf ("%d", &count);
  for (i=0; i<count; i++) {
    memset (flag, 0, sizeof(int)*N);
    scanf ("%d", &n);
    getchar();
    for (j=0; j<n; j++)
      fgets (s[j], sizeof(s[j][0])*M, stdin); 
    for (j=0; j<n; j++)
      fgets (d[j], sizeof(d[j][0])*M, stdin);

    /* find immobile sequence */
    for (j=n-1, k=n-1; j>=0; j--) {
      if (!strcmp (s[j],d[k])) { 
        flag[k] = 1;
        k -= 1;
      }
    }
    /* output mobile sequence */
    for (j=n-1; j>=0; j--) {
      if (flag[j] != 1)
        printf ("%s", d[j]);
    }

    printf ("\n");
  }
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值