manacher模板

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 41000000
#define p(a) putchar(a)
#define For(i,a,b) for(int i=a;i<=b;++i)

using namespace std;
char a[N],s[N];
int ans,n;
void in(int &x){
    int y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(int x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

void manacher(){
    n=n*2+1;
    vector<int> d1(n);
    for (int i=1,l=1,r=-1;i<=n;i++) {
        int k=(i>r)?1:min(d1[l+r-i],r-i);
        while (1<=i-k&&i+k<=n&&s[i-k]==s[i+k]) k++;
          d1[i]=k--;
          if(i+k>r){
          l=i-k;
          r=i+k;
        }
        ans=max(ans,2*d1[i]-1);
    }
}

signed main(){
    cin>>(a+1);
    n=strlen(a+1);
    For(i,1,2*n+1){
        if(i&1) s[i]='#';
        else s[i]=a[i>>1];
    }
    manacher();
    o(ans/2);p('\n');
    return 0;
}

 

posted @ 2019-11-07 13:57  WeiAR  阅读(183)  评论(0编辑  收藏  举报