Girls' research

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   art   

hdu3294:http://acm.hdu.edu.cn/showproblem.php?pid=3294

題意:就是給你一個串,然後求一個最長的迴文串,輸出起點及串,但是這裡在之前要轉化一下。

題解:轉化一下,就是簡單的Manacher演算法。

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int N=2e6+440; 7 char str1[N],str[N<<1]; 8 int rad[N]; 9 char temp;10 void Manacher(int *rad,char *str,int n){11    int i,mx=0,id;12    for(int i=1;i<n;i++){13     if(mx>i){14     rad[i]=min(rad[2*id-i],mx-i);15    }16    else17     rad[i]=1;18     for(;str[i-rad[i]]==str[i+rad[i]];rad[i]++){19         if(rad[i]+i>mx){20             mx=rad[i]+i;21             id=i;22         }23      }24    }25 }26 27 int main(){28   while(~scanf("%c %s",&temp,str1)){29        getchar();30       int nn=strlen(str1);31         int n=2*nn+2;32         str[0]=‘$‘;33       for(int i=0;i<=nn;i++){34           str[2*i+1]=‘#‘;35           str[2*i+2]=(str1[i]-temp+26)%26+‘a‘;36       }37       memset(rad,0,sizeof(rad));38       Manacher(rad,str,n);39       int ans=0,pos=0;40       for(int i=0;i<=n;i++){41         if(rad[i]>=3&&rad[i]>ans){42             ans=rad[i];43             pos=i;44         }45       }46        ans--;47       if(ans==-1)printf("No solution\n");48       else{49             int s=pos-ans+1,e=pos+ans-1;50              printf("%d %d\n",s/2-1,e/2-1);51         for(int i=pos-ans+1;i<=pos+ans-1;i++){52             if(str[i]==‘#‘)continue;53             printf("%c",str[i]);54         }55         puts("");56       }57   }58 }
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.