UVa 10152:SHELLSORT Data structure topics

Source: Internet
Author: User

Topic Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_ problem&problem=1093

Type of topic: data structure, linked list

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

Topic: Hill Sort, but nothing to do with Hill sort. There are a bunch of turtles, stacked from top to bottom, each with their own names. Then the topic gives two sequences, the first is the initial sequence, the second is the target sequence, the problem requires the least steps to the initial sequence into the target sequence, the conversion of the operation: each time can only select a turtle, the turtle out to put to the top, the other turtles are all landed a grid.

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45538.htm

To solve ideas:   To make the least steps, then ensure that the relative order is in line with the target sequence does not move (that is, the largest common child sequence do not move).   You can set two coordinates to point to the last of two arrays, and then enumerate forward, the same, two coordinates minus 1, if not the same, the initial array of coordinates minus one. Finally, the coordinates to the initial array move to the end, you will find that the target array coordinates have not yet reached an end, then the coordinates before this is not in line with the order, it is necessary to move the turtles.   Then the rest of the turtles, output in reverse order of the target array.

#include <string>  
#include <cstring>  
#include <iostream>  
#include <cstdio>  
using namespace std;  
    
Char origin[250][85], result[250][85];  
    
    
int main ()  
{  
    freopen ("Input.txt", "R", stdin);  
    int T, n,i,j;  
    scanf ("%d", &t);  
    GetChar ();  
        
    while (t--) {  
            
        scanf ("%d", &n);  
        GetChar ();  
            
        For (i=0 i<n; ++i) {  
            gets (origin[i]);  
        }  
        For (i=0 i<n; ++i) {  
            gets (result[i]);  
        }  
            
        int left = n-1,right=n-1;  
        while (left>=0 && right>=0) {  
            if (strcmp (Origin[left), Result[right])) {  
                --left;  
            }  
            else
                --right,--left;  
        }  
        while (right>=0) puts (result[right--]);  
        if (T) printf ("\ n");  
    }  
    return 0;  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.