吉哥系列故事——完美隊形II

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   art   

hdu4513:http://acm.hdu.edu.cn/showproblem.php?pid=4513

題意:給以一個序列,然後讓你求一個最長迴文序列的長度,這個序列的從左到最中間那個數是不降的,從中間那裡向右邊的話是不增的。

題解:用Manacher搞定,直接套模板還不行,還要做一些判斷。

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define  M 800050 6 using namespace std; 7 int  str1[M],str[M*2]; 8 int rad[M]; 9 void Manacher(int *rad, int  *str,int n){10   int i,mx=0,id;11   for(int i=1;i<n;i++){//下標從1開始12     if(mx>i){13      rad[i]=min(rad[2*id-i],mx-i);14     }15     else rad[i]=1;16     while(str[i+rad[i]]==str[i-rad[i]]){17         if(str[i+rad[i]]!=40){18             if(str[i+rad[i]]<=str[i+rad[i]-2])19                  rad[i]++;20             else21                 break;22         }23         else24             rad[i]++;25     }26         if(rad[i]+i>mx){27             mx=rad[i]+i;28             id=i;29         }30   }31 }32 int main(){33    int ans,cas;34    scanf("%d",&cas);35    while(cas--){36        int nn;37       scanf("%d",&nn);38    for(int i=0;i<nn;i++)39       scanf("%d",&str1[i]);40      memset(str,0,sizeof(str));41      int n=nn*2+2;42      str[0]=40;43      memset(rad,0,sizeof(rad));44      for(int i=0;i<=nn;i++){45          str[2*i+1]=40;46         str[2*i+2]=str1[i];47      }48      Manacher(rad,str,n);49          ans=1;50     for(int i=0;i<n;i++)51         ans=max(ans,rad[i]);52         printf("%d\n",ans-1);53    }54   return 0;55 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.