manacher

//找最长回文字串
#include <bits/stdc++.h>
using namespace std;
const int N=2e7+2e6+10;
void in(int &x){
    x=0;char c=getchar();
    int y=1;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
    x*=y;
}
void o(int x){
    if(x<0){x=-x;putchar('-');}
    if(x>9)o(x/10);
    putchar(x%10+'0');
}
int ans=0;
int n,m,i,r,p,f[N],g[N];char a[N],s[N];
void up(int&a,int b){if(a<b)a=b;}
int main() {
    //while (1) {
        scanf("%s", a + 1), n = strlen(a + 1);
        for (i = 1; i <= n; i++)s[i << 1] = a[i], s[i << 1 | 1] = '#';
        s[0] = '$', s[1] = '#', s[m = (n + 1) << 1] = '@';
        for (r = p = 0, f[1] = 1, i = 2; i < m; i++) {
            for (f[i] = r > i ? min(r-i, f[p * 2-i]):1;
            s[i-f[i]]==s[i + f[i]];
            f[i]++);
            if (i + f[i] > r)r = i + f[i], p = i;
        }
        for (i = 0; i <= m; i++)g[i] = 0;
        for (i = 2; i < m; i++)up(g[i-f[i] + 1],i + 1);
        for (i = 1; i <= m; i++)up(g[i], g[i-1]);
        ans = 0;
        for (i = 2; i < m; i += 2)ans=max(ans,g[i]-i);
        o(ans);putchar('\n');
    //}
}

posted @ 2020-11-02 17:41  yesuweiYYYY  阅读(54)  评论(0编辑  收藏  举报