首页 > 系统服务 > 详细

UVA10152 ShellSort【匹配】

时间:2019-02-24 12:07:39      阅读:209      评论:0      收藏:0      [点我收藏+]

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!
????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.
Input
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.
Output
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

问题链接UVA10152 ShellSort
问题简述:(略)
问题分析
????原始给定若干个字符串(字符串序列),另外给定这些字符串的要求顺序(也是字符串序列),可以做的操作是从中取出一个字符放到最前面,计算需要移动哪些字符串,使得字符串顺序调整为要求的顺序。
????将目标字符串从后往前与原始给定字符串进行匹配(也是从后往前),不能顺序匹配的那些字符串即为需要移动的字符串。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10152 ShellSort */

#include <bits/stdc++.h>

using namespace std;

const int N = 256;
char s[N][N], t[N][N];

int main()
{
    int k, n;
    scanf("%d", &k);
    while(k--) {
        scanf("%d", &n);
        getchar();
        for(int i = 0; i < n; i++)
            gets(s[i]);
        for(int i = 0; i < n; i++)
            gets(t[i]);

        int p = n - 1, q = n - 1;
        while(p >= 0) {
            if(!strcmp(t[q], s[p])) q--;
            p--;
        }

        while(q >= 0)
            printf("%s\n", t[q--]);
        printf("\n");
    }

    return 0;
}

UVA10152 ShellSort【匹配】

原文:https://www.cnblogs.com/tigerisland45/p/10425642.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!