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 K giving the number of test cases. Each test case consist on an integer n giving the number of turtles in the stack. The next n 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 n 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的更多相关文章
- 《K&R》中引用的几个排序算法(shellsort、)以及一个自己乱写的排序
留待期末考后更新... void shellsort(int v[], int n) { int gap, i, j, temp; ; gap > ; gap /= ) for(i = gap; ...
- Java基础知识强化57:经典排序之希尔排序(ShellSort)
1. 希尔排序的原理: 希尔排序(Shell Sort)是插入排序的一种.也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本.希尔排序是非稳定排序算法.该方法因DL.Shell于1959年提出 ...
- Foundation Sorting: Shellsort
/* Shell Sorting. * Implemention history:. * 2013-09-15, Mars Fu, first version. */ /* [Shell Sortin ...
- ShellSort
#include <bits/stdc++.h> using namespace std; #define MAXSIZE 200000 typedef int KeyType; type ...
- ShellSort uva
ShellSort He made each turtle stand on another one's back And he piled them all up in a nine-turtle ...
- 希尔排序(Shellsort)
首先,Shell是发明这个算法的人名,不是这个算法的思想或者特点. 希尔排序,也称为增量递减排序.基本思路,是把原来的序列,等效视为一个矩阵的形式.矩阵的列数,也称为宽度或者增量,记为w. 假设数组A ...
- 【算法】【排序】【插入类】希尔排序 ShellSort
#include<stdio.h> #include <time.h> #include<stdlib.h> int main(){ ]; //设立随机数 sran ...
- 直接插入排序与缩小增量插入排序(希尔排序ShellSort)
直接插入排序 要理解shell排序,首先要把直接插入排序的基础打扎实. 学习资料:白话经典算法系列之二 直接插入排序的三种实现.直接插入排序 根据我的思路,直接插入排序设置3重循环. 循环1:对 i= ...
- Java ShellSort
Java ShellSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational ...
- C#数据结构与算法系列(二十一):希尔排序算法(ShellSort)
1.介绍 希尔排序是希尔(Donald Shell)于1959年提出的一种排序算法.希尔排序也是一种插入排序,它是简单插入排序经过改进之后的一个更高效的版本,也称为缩小增量排序. 2.基本思想 希尔排 ...
随机推荐
- oracle数据库跨库查询
create public database link mylink connect to orclname identified by orclpasswd using 'ORCL'; drop p ...
- swift Enumerations
swift Enumerations enum.case.switch CaseIterable allCases 要区别枚举变量和关联值 枚举变量参与枚举运算: 关联值和rawvalue不参与. A ...
- C# HttpWebRequest Post Get 请求数据
Post请求 1 //data 2 string cookieStr = "Cookie信息"; 3 string postData = string.Format("u ...
- Xamarin绑定ios静态库
以下是官方的步骤介绍,我就不再一步步解释了 https://docs.microsoft.com/zh-cn/xamarin/ios/platform/binding-objective-c/walk ...
- Apache 和 Nginx 下的 URL 重写
URL 重写和重定向 URL 重写是将页面映射到本站另一页面, 而重定向则是将页面映射到另一主机(域名). 其中临时重定向(R=302)和永久重定向(R=301)都是亲搜索引擎的, 是 SEO 的重要 ...
- git学习(2)----入门
一.git.github和gitlab的区别 Git诞生于2005年,大神Linus的作品,Github诞生于2008年,没有Git就没有GitHub,Github已成为全球最大的代(tong)码(x ...
- [Algorithm] 8. Rotate String
Description Given a string and an offset, rotate string by offset. (rotate from left to right) Examp ...
- 【ssm】spring功能讲解
概览 Spring5框架包含许多特性,负责管理项目中的所有对象,并被很好地组织在下图所示的模块中 核心容器:由spring-beans.spring-core.spring-context.sprin ...
- Laravel学习:请求到响应的生命周期
Laravel请求到响应的整个执行过程,主要可以归纳为四个阶段,即程序启动准备阶段.请求实例化阶段.请求处理阶段.响应发送和程序终止阶段. 程序启动准备阶段 服务容器实例化 服务容器的实例化和基本注册 ...
- save density, pressure, velocity, temperature contour at one slice in xy plane-- paraview with batch Python scripts
#### import the simple module from the paraviewfrom paraview.simple import *#### disable automatic c ...