UVa 10152 - ShellSort
来源:互联网 发布:老mac安装10.12 编辑:程序博客网 时间:2024/06/10 14:12
思路:水...
#include <cstdio>#include <queue>#include <cstring>#include <iostream>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <string>#include <set>#include <ctime>#include <cmath>#include <cctype>using namespace std;#define maxn 100000#define LL long longint cas=1,T;char a[250][120];char b[250][120];int main(){scanf("%d",&T);while (T--){int n;scanf("%d",&n);getchar();for (int i = 0;i<n;i++)gets(a[i]);for (int i = 0;i<n;i++)gets(b[i]);int p = n-1,q=n-1;while (p>=0){if (!strcmp(a[p],b[q]))q--;p--;}while (q>=0) printf("%s\n",b[q--]);printf("\n");}//freopen("in","r",stdin);//scanf("%d",&T);//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);return 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
0 0
- uva 10152 shellsort
- Uva 10152 ShellSort
- uva 10152 - ShellSort
- UVa 10152 - ShellSort
- uva-10152 - ShellSort
- UVa 10152 - ShellSort
- uva 10152 - ShellSort
- 数据结构 uva-10152 - ShellSort
- UVa 10152 - ShellSort
- UVa 10152 - ShellSort
- uva 10152ShellSort
- uva 10152 ShellSort
- UVa 10152 - ShellSort
- uva 10152 ShellSort
- uva 10152 ShellSort
- UVa 10152 ShellSort
- uva 10152 ShellSort
- uva 10152 - ShellSort
- 求方程所确定的隐函数y在 x=0处的导数
- JavaScript 实现暴击事件
- javascript里getFullYear()、getYear()的区别
- C++错误输入的处理--整型错误输入字符串
- Redis集群监控工具之RedisLive
- UVa 10152 - ShellSort
- Vsftp配置文件的设定值说明讲解
- 《leetCode》:largestRectangleArea
- 简单选择排序 java实现
- 初等数论_1 2016.3.30
- JSCH专题
- 最简单的反射型XSS拿cookie
- 第5周项目2—游戏中的角色类(1)
- Linux(centos6.7)下vsftp服务的配置