uva10152-ShellSort

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 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 <vector>
#include <string>
#include <algorithm>
using namespace std;

vector<string> v1;
vector<string> v2;
vector<string> v;
string s1, s2;
int size;
int n;

int OneTraverse(void);

int main(void){
	string s1, s2;
	int size;
	int n;

#ifndef ONLINE_JUDGE
	freopen("f://infile.txt", "r", stdin);
#endif
	cin >> size;
	for(int i = 0; i < size; i++){
		cin >> n;
		cin.ignore(100, '\n');
		v1.clear();
		v2.clear();
		for(int j = 0; j < n; j++){
//			cin.ignore(100, '\n');
			getline(cin, s1);
			v1.insert(v1.begin(), s1);
		}
		for(int j = 0; j < n; j++){
//			cin.ignore(100, '\n');
			getline(cin, s2);
			v2.insert(v2.begin(), s2);
		}
		int s = 0;
		while(1){
			if(OneTraverse()){
				break;
			}
		}
		cout << endl;
	}
	return 0;
}

int OneTraverse(void){
	v.clear();

	size_t k;
	size_t m;
	size_t preM = 0;
	vector<string> tempV;
	for(k = 0; k < v2.size(); k++){
		tempV.clear();
		for(m = preM; m < v1.size(); m++){
			if(v1[m] == v2[k]){
				preM = m+1;
				break;
			}
			else{
				tempV.push_back(v1[m]);
			}

		}
		if(m < v1.size())
			v.insert(v.end(), tempV.begin(), tempV.end());
		else
			break;
	}
	if(v.size() == 0)
		return 1;
	size_t kk;
	for(kk = k; kk < v2.size(); kk++){
		size_t index;
		for(index = 0; index < v.size(); index++){
			if(v[index] == v2[kk]){
				string temp;
				temp = v[index];
				v1.erase(find(v1.begin(), v1.end(), temp));
				v1.push_back(temp);
				cout << temp << endl;
				break;
			}
		}
		if(index == v.size())
			break;
	}
	return 0;
}

uva10152-ShellSort

时间: 2024-08-27 10:57:07

uva10152-ShellSort的相关文章

ShellSort

code: 1 @SuppressWarnings({ "rawtypes", "unchecked" }) 2 public static void shellSort(Object[] array) { 3 int len = array.length; 4 int h = 1; 5 while (h <= len / 3) 6 h = h * 3 + 1; 7 while (h > 0) { 8 for (int i = h; i < len

排序--ShellSort 希尔排序

希尔排序 no 实现 希尔排序其实就是插入排序.只不过希尔排序在比较的元素的间隔不是1. 我们知道插入排序 都是 一个一个和之前的元素比较.发现比之前元素小就交换位置.但是希尔排序可能是和前第n个元素比较,如果发现比前第n个元素小就和前第n个元素交换位置.具体看下图 第一趟比较.n是为5.也就是说每个数和前面第5个数比较.如果发现小于前面第5个数的话.交换位置. 所以我们看到 72 比 592 小. 所以交换位置. 283 比 348 小.继续交换 第二趟比较. n 是2.同理 最后一次比较.n

UVa 10152 - ShellSort 题解

按他的方法排序,每次移动一个数到顶点,排成需要的序列. 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! T

ShellSort uva

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 rearra

UVA ShellSort

题目如下: 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

希尔排序---shellsort

#include <iostream> using namespace std; void shellSort(int a[], int n) { int i, j, gap; for(gap = n/2; gap > 0; gap /= 2)//间隔,逐次递减一半 { for(i = 0; i < gap; i++)//从49到76,逐步递增,也就是分组数.每次循环对一组数完成排序 { for(j = i + gap; j < n; j += gap)//对这个一组内的,间

【算法】【排序】【插入类】希尔排序 ShellSort

#include<stdio.h> #include <time.h> #include<stdlib.h> int main(){ int a[15]; //设立随机数 srand(time(0)); for(int i=0;i<15;i++){ a[i]=rand()%(30); //随机数范围0到29之间 } //数组a的大小 int size=sizeof(a)/4; //打印数组信息 for(int i=0;i<size;i++){ printf(

&lt;排序算法&gt; 希尔排序ShellSort

1.核心思想:希尔排序是插入排序的一种,是直接插入排序的一种改进版本,他们同属于插入排序类.这是一个不稳定的排序算法.采取跳跃分割的策略:将相距某个“增量”的记录组成一个子序列,这样才能保证在子序列内分别进行直接插入排序后得到的结果是基本有序而不是局部有序.2.代码实现: 1 #include<iostream> 2 using namespace std; 3 4 void PrintArr(int arr[],int len); 5 void ShellSort(int arr[],int

ShellSort 希尔排序

# ShellSort希尔排序_Python实现 def shell_sort(li): n = len(li) # gap间隔为长度除2 gap = n // 2 while gap > 0: for i in range(gap, n): while i >= gap and li[i - gap] > li[i]: li[i], li[i - gap] = li[i - gap], li[i] i -= gap gap //= 2 return li list = [1, 55,

UVa 10152 - ShellSort

题目:给你一个现有字符串的序列,以及一个目标字符串的序列,每次操作可以把一个单词置顶, 问把当前串变成目标串,要操作几次. 分析:排序.因为每次可以使一个单词置顶,所以每个单词最多移动一次就可以变为目标串. 从后向前找到每个目标串元素对应元素的当前串位置(每个只能在"上一个"之前): 能找到的元素个数,就是不需要改动的个数,其他均要移动,对应目标串中剩余的元素: 剩余的目标串倒序输出,即为操作顺序. 定义两个指针分别从两串序列的尾部向头部移动,如果匹配成功同时前进: 不成功,当前传指针