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. hashtable的用法

    C#中哈希表(HashTable)的用法详解 1.  哈希表(HashTable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器 ...

  2. oracle 入门笔记---分区表的分区交换

    本文参考来自作者:蓝紫 详细内容请阅读原文 : http://www.cnblogs.com/lanzi/archive/2013/01/24/2875838.html 在oracle 11.2环境下 ...

  3. 本地调试hbase

    需求说明 如果要本地调试Hbase程序,那么可以用本地连接集群的方式 配置文件 在maven里,配置文件cluster.properties放在target/classes里 cluster.prop ...

  4. MS-DOS Batch Script Template

    @echo off @setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION @rem Name: @rem Purpose: @rem @rem Autho ...

  5. JMeter在linux上分布式压测步骤(二)

    哈喽,我又来了~ 前提:三台linux虚拟机,一台作为master,另外两台作为slave. 一.server端 1.修改1099端口,client和server通信的端口,可以不修改,默认就是109 ...

  6. vue基础---介绍

    (1)声明式渲染 Vue.js 的核心是采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统: ①文本 <div id="app"> {{ message }} & ...

  7. luogu P4137 Rmq Problem / mex 主席树 + 思维

    Code: #include<bits/stdc++.h> #define maxn 200001 using namespace std; void setIO(string s) { ...

  8. [Luogu] P3701 「伪模板」主席树

    题目背景 byx和手气君都非常都非常喜欢种树.有一天,他们得到了两颗奇怪的树种,于是各自取了一颗回家种树,并约定几年后比一比谁种出来的树更加牛x. 题目描述 很快,这棵树就开花结果了.byx和手气君惊 ...

  9. pygame 方块随机飞舞动画

    import pygame import random # default WIDTH=1280 HEIGHT=1060 FPS=60 sum=0 # set color WHITE=(255,255 ...

  10. java加载properties文件的六种方法总结

    java加载properties文件的六种方法总结 java加载properties文件的六中基本方式实现 java加载properties文件的方式主要分为两大类:一种是通过import java. ...