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的更多相关文章

  1. 《K&R》中引用的几个排序算法(shellsort、)以及一个自己乱写的排序

    留待期末考后更新... void shellsort(int v[], int n) { int gap, i, j, temp; ; gap > ; gap /= ) for(i = gap; ...

  2. Java基础知识强化57:经典排序之希尔排序(ShellSort)

    1. 希尔排序的原理: 希尔排序(Shell Sort)是插入排序的一种.也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本.希尔排序是非稳定排序算法.该方法因DL.Shell于1959年提出 ...

  3. Foundation Sorting: Shellsort

    /* Shell Sorting. * Implemention history:. * 2013-09-15, Mars Fu, first version. */ /* [Shell Sortin ...

  4. ShellSort

    #include <bits/stdc++.h> using namespace std; #define MAXSIZE 200000 typedef int KeyType; type ...

  5. ShellSort uva

    ShellSort He made each turtle stand on another one's back And he piled them all up in a nine-turtle ...

  6. 希尔排序(Shellsort)

    首先,Shell是发明这个算法的人名,不是这个算法的思想或者特点. 希尔排序,也称为增量递减排序.基本思路,是把原来的序列,等效视为一个矩阵的形式.矩阵的列数,也称为宽度或者增量,记为w. 假设数组A ...

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

    #include<stdio.h> #include <time.h> #include<stdlib.h> int main(){ ]; //设立随机数 sran ...

  8. 直接插入排序与缩小增量插入排序(希尔排序ShellSort)

    直接插入排序 要理解shell排序,首先要把直接插入排序的基础打扎实. 学习资料:白话经典算法系列之二 直接插入排序的三种实现.直接插入排序 根据我的思路,直接插入排序设置3重循环. 循环1:对 i= ...

  9. Java ShellSort

    Java ShellSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational ...

  10. C#数据结构与算法系列(二十一):希尔排序算法(ShellSort)

    1.介绍 希尔排序是希尔(Donald Shell)于1959年提出的一种排序算法.希尔排序也是一种插入排序,它是简单插入排序经过改进之后的一个更高效的版本,也称为缩小增量排序. 2.基本思想 希尔排 ...

随机推荐

  1. JVM 参数含义

    JVM参数的含义 实例见实例分析 参数名称 含义 默认值   -Xms 初始堆大小 物理内存的1/64(<1GB) 默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40%时,J ...

  2. Ubuntu搭建LAMP开发环境

    1.安装Apache sudo apt-get install apache2 测试: 浏览器访问 (如:http://localhost),出现It Works!网页. 查看状态: service ...

  3. Vue.js 观察者(watch)

    Vue.js 观察者(watch) watch 属性用于监视 vue 实例上的数据变动,并相应的改变其他变量的值. 用法 实例 1 <!DOCTYPE html> <html> ...

  4. (C/C++学习)20.基于C++改进的单目标遗传算法

    说明:在学习生活中,经常会遇到各种各样的最优问题,其中最常见的就是求某个多维(多个自变量)函数在各个自变量各取何值时的最大值或最小值:例如求函数 f(x) = (x-5)2+(y-6)2+(z-7)2 ...

  5. UVA - 12661 Funny Car Racing (Dijkstra算法)

    题目: 思路: 把时间当做距离利用Dijkstra算法来做这个题. 前提:该结点e.c<=e.a,k = d[v]%(e.a+e.b); 当车在这个点的1处时,如果在第一个a这段时间内能够通过且 ...

  6. Gym - 101670H Go Northwest!(CTU Open Contest 2017 思维题+map)

    题目: Go Northwest! is a game usually played in the park main hall when occasional rainy weather disco ...

  7. Linux---文件目录管理

    1. Linux文件目录架构 Linux的目录结构与win的目录有很大不同,首先,没有盘符的概念:然后Linux使用斜杠/标识目录,Linux首先建立一个根目录,然后将其他文件系统挂载到这个目录下. ...

  8. 整理推荐比较好用的具有书签搜索功能的chrome插件

    平时在整理学习chrome插件的过程中,经常会去试用各种大家推荐的插件.在去年我们的一篇文章:推荐六款好用的书签收藏夹剪藏型管理插件.这里面介绍的是收藏书签的插件.而随着我们使用chrome越来越频繁 ...

  9. LINUX-光盘

    cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force 清空一个可复写的光盘内容 mkisofs /dev/cdrom > ...

  10. BZOJ 5106 [CodePlus2017]汀博尔

    [题解] 二分答案.r要设好,不能随便设置为max(s,len),不然check的时候会爆long long #include<cstdio> #include<algorithm& ...