HDU3068 最长回文 manacher

#include <bits/stdc++.h>
using namespace std;
const int m = 110010;
char s[m*2];
int mp[m*2];
char ma[2*m];
int l,len;
void manacher(){
    l = 0;
    ma[l++] = '$';
    ma[l++] = '#';
    for (int i = 0;i < len;++i){
        ma[l++] = s[i];
        ma[l++] = '#';
    }
    ma[l] = 0;
    int mx= 0,id = 0;
    for (int i = 0;i < l;++i){
        mp[i] = mx > i ? min(mp[2*id-i],mx-i) : 1;
        while(ma[i+mp[i]] == ma[i-mp[i]]) mp[i]++;
        if (i+mp[i] > mx){
            mx = i+mp[i];
            id = i;
        }
    }
}

int main(){
    while(~scanf("%s",s)){
        len = strlen(s);
        manacher();
        int ans = 0;
        for (int i = 0;i < 2*len+2;i++){
            ans = max(mp[i]-1,ans);
        }
        printf("%d\n",ans);
    }
}

 

posted @ 2018-08-27 19:17  mizersy  阅读(133)  评论(0编辑  收藏  举报