• UVa 10152 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
    

    给一摞字符串,操作是可以从中间取一个拿到最上面,问最少移动几个字符串可以将原来的一摞字符串的顺序调整到要求的顺序

    我的做法是:每次在原字符串组和目标字符串组中删去目标字符串组中最上面的那个字符串,然后比较余下的字符串组是否相同,如果不相同则继续删除,直到相同为止

      相同后,计算出一共删了m个字符串,然后将目标字符串组中的前m个字符串倒序输出即可(因为将字符串移到最顶部的操作相当于将这个串入栈,越先移动的串越靠下)

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<string>
      4 
      5 using namespace std;
      6 
      7 typedef struct node
      8 {
      9     string s;
     10     struct node *next;
     11 } NODE;
     12 
     13 string s_original[220],s_required[220];
     14 
     15 int main()
     16 {
     17     int kase;
     18 
     19     scanf("%d",&kase);
     20 
     21     while(kase--)
     22     {
     23         int n;
     24         NODE *original,*required,*current;
     25 
     26         scanf("%d",&n);
     27         getchar();
     28 
     29         for(int i=0;i<n;i++)
     30         {
     31             getline(cin,s_original[i]);
     32             NODE *tmp=new NODE;
     33             tmp->s=s_original[i];
     34             tmp->next=NULL;
     35             if(i==0)
     36                 current=original=tmp;
     37             else
     38             {
     39                 current->next=tmp;
     40                 current=current->next;
     41             }
     42         }
     43 
     44         for(int i=0;i<n;i++)
     45         {
     46             getline(cin,s_required[i]);
     47             NODE *tmp=new NODE;
     48             tmp->s=s_required[i];
     49             tmp->next=NULL;
     50             if(i==0)
     51                 current=required=tmp;
     52             else
     53             {
     54                 current->next=tmp;
     55                 current=current->next;
     56             }
     57         }
     58 
     59         int ans=0;
     60         bool same=false;
     61 
     62         while(!same)
     63         {
     64             NODE *current_a=original,*current_b=required;
     65 
     66             same=true;
     67             while(current_a&&current_b)
     68             {
     69                 if(current_a->s!=current_b->s)
     70                 {
     71                     same=false;
     72                     break;
     73                 }
     74                 current_a=current_a->next;
     75                 current_b=current_b->next;
     76             }
     77 
     78             if(!same)
     79             {
     80 
     81                 ans++;
     82                 NODE *tmp=required;
     83                 required=required->next;
     84                 string s_tmp=tmp->s;
     85                 delete tmp;
     86                 current_a=original;
     87                 if(current_a->s==s_tmp)
     88                 {
     89                     tmp=original;
     90                     original=original->next;
     91                     delete tmp;
     92                 }
     93                 else
     94                 {
     95                     while(current_a->next)
     96                     {
     97                         if(current_a->next->s==s_tmp)
     98                         {
     99                             tmp=current_a->next;
    100                             current_a->next=tmp->next;
    101                             delete tmp;
    102                             break;
    103                         }
    104                         current_a=current_a->next;
    105                     }
    106                 }
    107             }
    108         }
    109 
    110         for(int i=ans-1;i>=0;i--)
    111             cout<<s_required[i]<<endl;
    112         putchar('
    ');
    113 
    114     }
    115 
    116     return 0;
    117 }
    [C++]
  • 相关阅读:
    为什么大多Virtual Globe程序纵向旋转效率比较低
    惠普卖印刷服务 GIS卖什么?
    OpenLayers的新功能:矢量支持
    Google部分开源GMap API
    为OpenLayers 2.3添加Overview窗口
    从Grid控件到GIS软件
    GIS(数据)浏览器的点点滴滴
    ArcGIS 9.3和ArcGIS 10,一点感想
    关注:Pitney Bowes以4.08亿美金收购Mapinfo
    ArcGIS Server安装的几个问题
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3536731.html
Copyright © 2020-2023  润新知