UVa 10152 – ShellSort 資料結構專題

來源:互聯網
上載者:User
10152 - ShellSort 18905 34.82% 2697 81.79%


題目連結:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=1093

題目類型: 資料結構, 鏈表

範例輸入

23YertleDuke of EarlSir LancelotDuke of EarlYertleSir Lancelot9YertleDuke of EarlSir LancelotElizabeth WindsorMichael EisnerRichard M. NixonMr. RogersFord PerfectMackYertleRichard M. NixonSir LancelotDuke of EarlElizabeth WindsorMichael EisnerMr. RogersFord PerfectMack

範例輸出

Duke of EarlSir LancelotRichard M. NixonYertle

題目大意: 題目時希爾排序,但是和希爾排序無關。 有一堆烏龜, 從上到下疊好,各自有自己的名字。 然後題目給出兩個序列, 第一個是初始序列,第二個是目標序列, 題目要求是以最少的步驟把初始序列轉換成目標序列,轉換的操作為:每次只能選中一隻烏龜,把這個烏龜抽出來放到最頂端,其它的烏龜則全部降落一格。  


解題思路:  要使步驟最少,那麼要保證相對順序是符合目標序列的不要進行移動(即最大公用子序列不要進行移動)。  可以設定兩個座標指向兩個數組的最後一個,然後往前枚舉,碰到相同的,則兩個座標都減1, 如果不相同的,則初始數組的座標減一。最後指向初始數組的座標移動到了盡頭,會發現目標數組的座標還沒到盡頭,那麼在這個座標之前的都是不符合順序的,就要移動這些烏龜。
 然後剩下的這些烏龜, 按目標數組的逆序輸出即可。


#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;}

——      生命的意義,在於賦予它意義。 

                   原創  http://blog.csdn.net/shuangde800  , By
  D_Double



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.