zoukankan      html  css  js  c++  java
  • 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, '
    ');
    		v1.clear();
    		v2.clear();
    		for(int j = 0; j < n; j++){
    //			cin.ignore(100, '
    ');
    			getline(cin, s1);
    			v1.insert(v1.begin(), s1);
    		}
    		for(int j = 0; j < n; j++){
    //			cin.ignore(100, '
    ');
    			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;		
    }

  • 相关阅读:
    springmvc单Redis实例实现分布式锁(解决锁超时问题)
    出现Please make sure you have the correct access rights and the repository exists.问题解决
    微信小程序确保获取到token值之后再执行请求
    微信小程序api封装
    微信小程序 没有找到可以构建的npm包
    vscode开发微信小程序使用less(插件Easy WXLESS)
    深入理解移动端响应式(非原创)
    git push后出现错误 ![rejected] master -> master(non-fast-forward) error:failed to push some refs to 'XXX'
    微信小程序星星评价
    更改微信小程序的组件默认样式
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6943460.html
Copyright © 2011-2022 走看看