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. Sonar 规则

    bug类型: 1.".equals()" should not be used to test the values of "Atomic" classes. ...

  2. Spring与Struts2集成开发

    Struts2和Spring都是不错的开源框架,Spring与Struts2集成开发,把二者结合在一起使用,开发效果更佳,效率杠杠的.下面介绍一下如何将Spring与Struts2集成在一起开发.分七 ...

  3. python logger日志

    直接上代码 import logging import logging.handlers import datetime import time import threading from conf. ...

  4. git学习(3)----git 新建分支并提交本地代码到远程分支

    一.步骤 1.在gitlab上创建一个issue,issue一般来说是版本发布说明.比如本次更新了什么功能,修复了什么bug什么的. 2.然后在本地创建一个branch,或者直接在gitlab上申请m ...

  5. LeetCode_16 3SumCloest

    3Sum Closest Given an array nums of n integers and an integer target, find three integers in nums su ...

  6. 深入理解PHP之strpos

    概述 在php中经常用 strpos 判断字符串是否在另一个字符串中存在, 本文介绍 strpos 函数及其实现. strpos应用 <?php /* strpos示例 */ // test e ...

  7. buf.readInt8()

    buf.readInt8(offset[, noAssert]) offset {Number} 0 noAssert {Boolean} 默认:false 返回:{Number} 从该 Buffer ...

  8. 洛谷 3285 [JLOI2014]松鼠的新家

    [题解] 给出一条路径,问树上的点被经过了几次. 显然树剖之后树上差分就好了. #include<cstdio> #include<algorithm> #define N 3 ...

  9. Uva12657 Boxes in a Line

    题目链接:传送门 分析:每次操作都会花费大量时间,显然我们只需要关注每个元素的左边是啥,右边是啥就够了,那么用双向链表,l[i]表示i左边的数,r[i]表示i右边的数,每次操作模拟一下数组的变化就好了 ...

  10. 纯JSP实现简单微信开发后台

    %@ page import="java.net.*" % %@ page import="java.math.*" % %@ page import=&quo ...