Uva 10152 ShellSort

点击打开链接

题目意思:有一堆的乌龟,输出一堆乱序的乌龟,然后输入一个正确的顺序,要求找到一种最小步骤的方法使得第一堆变成第二堆,每一次可以把乌龟移到第一个位置,输出该方法步骤.

解题思路:我们可以用两个char 数组来存储第一和第二堆的乌龟顺序,然后用两个int 数组num和tem来存储乌龟的顺序编号.我们知道只要从后面往前遍历tem数组就可以找到最小的步骤,然后利用栈的先进后出的性质(由上面可知,乌龟最后一个肯定是最先移动的)

代码:

//UVA10152
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <map>
#include <algorithm>
using namespace std;

char ch[210][100] , temp[210][100];
int  num[210] , tem[210];

void output(int n){
    int i , j;
    memset(tem , 0 ,sizeof(tem));
    for(i = 0 ; i < n ;i++){
        for(j = 0 ; j < n ; j++){
            if(strcmp(temp[i] , ch[j]) == 0)
                tem[i] = num[j];
        }
    }
    for(i = n-2 ;i >= 0 ; i--){
        if(tem[i] > tem[i+1])
            break;
    }
    stack<char*>s;
    for(j = 0 ;j <= i ;j++)
        s.push(temp[j]);
    while(!s.empty()){
        cout<<s.top()<<endl;
        s.pop();
    }
    cout<<endl;
}
int main(){
    int t , n , i , j;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        getchar();
        memset(num , 0 , sizeof(num));
        for(i = 0 ;i < n ; i++){
            gets(ch[i]);
            num[i] = i;
        }
        for(i = 0 ; i < n ; i++){
            gets(temp[i]);
        }
        output(n);
    }
    return 0;
}
时间: 2024-09-20 04:04:16

Uva 10152 ShellSort的相关文章

UVa 10152 ShellSort (想法题)

10152 - ShellSort Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=1093 He made each turtle stand on another one's back And he piled them all up in a nine

UVa 10152:ShellSort 数据结构专题

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=1093 题目类型: 数据结构, 链表 样例输入: 2 3 Yertle Duke of Earl Sir Lancelot Duke of Earl Yertle Sir Lancelot 9 Yertle Duke of Earl Sir Lanc

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 701 The Archeologists&#039; Dilemma: 数学及枚举

701 - The Archeologists' Dilemma Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=642 An archeologist seeking proof of the presence of extraterrestrials i