UVa 10152 - ShellSort

来源:互联网 发布:网络主播幕后团队 编辑:程序博客网 时间:2024/05/17 20:47

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 integer K giving the number of test cases. Each test case consist on an integern 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 nextn 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 <string.h>#include <stdio.h>int main(){    int k,n,c[205],i,j;    char a[205][100],b[205][100];    scanf("%d",&k);    while(k--)    {        scanf("%d",&n);        getchar();        for(i=0;i<n;i++)            gets(a[i]);        for(j=0;j<n;j++)            gets(b[j]);        for(i=0;i<n;i++)               for(j=0;j<n;j++)                if(!strcmp(b[j],a[i]))                {                    c[j]=i;                    break;                }        for(i=n-1;i>=0;i--)            if(c[i]<c[i-1])break;        for(i--;i>=0;i--)            puts(b[i]);        printf("\n");    }   return 0;}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果4s微信版本过低怎么办 微信版本太低无法登录怎么办 苹果4微信版本低登录不了怎么办 安卓手机微信版本低登录不了怎么办 苹果微信版本低登录不了怎么办 安装包与当前版本不兼容怎么办 游戏与苹果手机系统不兼容怎么办 闲鱼买家申请退款不退货怎么办 在淘宝买的战地1登录不了怎么办 支付宝转账到注销的账号怎么办 支付宝用户支付密码被锁定怎么办 淘宝提现需要的手机宝令怎么办 手机换号码了支付宝账号怎么办 卖家收到货迟迟不给退款怎么办 闲鱼东西已发出买家申请退款怎么办 如果东西在路上买家要退款怎么办 卖家收到退的货后不退款怎么办 扣48分淘宝被限制登录怎么办 淘宝网申请退款卖家不理怎么办 高考生如果把登录密码忘记了怎么办 普高考生登录密码忘记了怎么办 淘宝买的流量卡然后网卡怎么办? 忘记了qq号绑定了手机怎么办 微信绑定qq号忘记了怎么办 扣扣密码忘了手机号换了怎么办 如何更改微信登录密码忘记了怎么办 阿里卖家不肯同意退货退款怎么办 新买的号码被绑定微信怎么办 如果换手机原来旧手机支付宝怎么办 支付宝注册证件已过有效期怎么办 支付宝账号是手机号换号怎么办 淘宝网卖假药伤了别人身体怎么办 微信手机登录收不到验证码怎么办 淘宝店的宝贝没有尺码选项怎么办 淘宝账号被保护要自助开通怎么办 小米平板经常出现系统无响应怎么办 淘宝买家被取消运费险了怎么办 新开的淘宝直播店没有流量怎么办 淘宝买的东西一直不发货怎么办 在淘宝上买了东西不发货怎么办 宝贝好几天拉一次吃的也少怎么办