uva 10152 ShellSort

来源:互联网 发布:传智播客java百度云 编辑:程序博客网 时间:2024/05/18 00:10

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 giving the number of test cases. Each test case consist on an integer giving the number of turtles in the stack. The next 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 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

23YertleDuke of EarlSir LancelotDuke of EarlYertleSir Lancelot9YertleDuke of EarlSir LancelotElizabeth WindsorMichael EisnerRichard M. NixonMr. RogersFord PerfectMackYertleRichard M. NixonSir LancelotDuke of EarlElizabeth WindsorMichael EisnerMr. RogersFord PerfectMack

Sample Output

Duke of EarlSir LancelotRichard M. NixonYertle


题目大意:给出两个字符串序列,原先的排列序列和转变后的序列。每次移动字符串只能移动到最上层。求最优的移动方案,输出所移动字符串(按移动的先后顺序)


注意:每组测试间需要输出空格。


解题思路:遍历两个字符串序列,得出转变后序列的连续递减序列,输出剩余的字符串。


#include<iostream>#include<string.h>#include<stdio.h>using namespace std;#define N 1000#define M 500char str[N][M];char tem[N][M];int bo[N];int main(){int i;int n;cin>>n;while(n--){//Init.memset(str,0,sizeof(str));memset(tem,0,sizeof(tem));memset(bo,0,sizeof(bo));int m;cin>>m;getchar();for(i = 0;i < m; i++)gets(str[i]);for(i = 0;i < m; i++)gets(tem[i]);int s=m-1;int t=m-1;while(1){if(strcmp(tem[t],str[s])==0){bo[t]=1;t--;s--;}elses--;if(s==-1)break;}for(;t>=0;t--)cout<<tem[t]<<endl;cout<<endl;}return 0;}

原创粉丝点击