uva10152(shellsort)—线性表
2014-08-02 08:29
459 查看
Problem D: ShellSort
He made each turtle stand on another one's backAnd 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 turtlecan 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
从目标的最低端和原始串的最低端依次比较,如果到最后目标串有剩余未比较的或是比较不相等的,则输出
#include<stdio.h> #include<string.h> #define MAX 500 char orginal[MAX][90]; char desire[MAX][90]; int main(){ int k; scanf("%d",&k); while(k--){ int n; scanf("%d",&n); int i; getchar(); for(i=0;i<n;i++){ gets(orginal[i]); } for(i=0;i<n;i++){ gets(desire[i]); } int j=n-1; for(i=n-1;i>=0&&j>=0;){ if(strcmp(desire[i],orginal[j])==0){ i--; j--; } else j--; } while(i>=0){ printf("%s\n",desire[i]); i--; } printf("\n"); } return 0; }
相关文章推荐
- Uva 10152 - ShellSort//排序,线性表
- uva 10152 ShellSort 解题报告
- uva 10152 ShellSort
- (字符串的映射与匹配7.3.13)UVA 10152 ShellSort(将初始序列转化成目标序列所需的最少次数,前提条件是:每一个元素只能移动栈顶)
- ShellSort UVA10152
- uva 10152 ShellSort(模拟)
- UVA - 10152 ShellSort
- uva 10152 ShellSort 龟壳排序(希尔排序?)
- UVa 10152 - ShellSort 题解
- uva 10152 ShellSort 龟壳排序(希尔排序?)
- UVa 10152 - ShellSort 龟壳排序
- UVa Problem 10152 ShellSort (龟壳排序)java实现
- UVa OJ ShellSort 10152 龟壳排序
- UVa 10152 - ShellSort
- UVa 10152 ShellSort解题报告
- UVa-10152 ShellSort(模拟)
- UVa 10152 - ShellSort 数据结构专题
- UVa Problem 10152 ShellSort (龟壳排序)
- UVA 10152-ShellSort(映射+栈)
- UVA ShellSort