双倍回文

双倍回文

#include <bits/stdc++.h>
using namespace std;
const int M=1e6+5;

char s[M];
int p[M],n=1,ans=0;

void Manacher(string t) {
    s[0]='@',s[1]='#';
    for(auto i:t)s[++n]=i,s[++n]='#';
    for(int i=1,mid=0,r=0;i<=n;i++) {
        if(i<=r)p[i]=min(p[2*mid-i],r-i);
        while(s[i-p[i]]==s[i+p[i]]) {
            p[i]++;
            if(i+p[i]-1>r&&s[i]=='#'&&(p[i]-1)%4==0) {
                if(p[i-(p[i]-1)/2]-1>=(p[i]-1)/2)ans=max(ans,p[i]-1);
            }
        }
        if(i+p[i]>r)r=i+p[i]-1,mid=i;
    }
}
//实质上是枚举了每一个出现过的回文串的
//也就是对每一个回文串都判断一下

int main() {
    int n;
    string t;
    cin>>n>>t;
    Manacher(t);
    cout<<ans<<'\n';
    return 0;
}
posted @ 2023-02-14 20:00  basicecho  阅读(28)  评论(0)    收藏  举报