uva 10152 ShellSort

942 篇文章 2 订阅

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

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<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
#define N 1000
#define M 500

char 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--;
			}
			else
				s--;
			if(s==-1)	break;
		}

		for(;t>=0;t--)
			cout<<tem[t]<<endl;
		cout<<endl;
	}
	return 0;}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值