51nod1089 最長迴文子串 V2(Manacher演算法)

來源:互聯網
上載者:User

1089 最長迴文子串 V2(Manacher演算法) 基準時間限制:1 秒 空間限制:131072 KB 分值: 0  難度:基礎題  收藏  關注 迴文串是指aba、abba、cccbccc、aaaa這種左右對稱的字串。 輸入一個字串Str,輸出Str裡最長迴文子串的長度。 Input

輸入Str(Str的長度 <= 100000)
Output
輸出最長迴文子串的長度L。
Input樣本
daabaac
Output樣本
5


#include<stdio.h>#include<string.h>char s[200010];int p[200010];int min(int a,int b){return a<b?a:b;}void f(){int l=strlen(s);for(int i=l;0<i;i--){s[i*2+1]='#';s[i*2]=s[i-1];}s[0]='*',s[1]='#';}int Manacher(){f();int  mx = 0, id = 0,max=1;memset(p, 0, sizeof(p));for (int i = 1; s[i] != '\0' ; i++) {    p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;    while (s[i + p[i]] == s[i - p[i]])         p[i]++;    if (i + p[i] > mx)     {        mx = i + p[i];        id = i;    }    max=max>p[i]?max:p[i];}return max-1;}int main(){int l,i;while(scanf("%s",s)!=EOF)printf("%d\n",Manacher());return 0;}


聯繫我們

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