题目:manacher算法
思路:模板题
代码:
#include<bits/stdc++.h>
using namespace std;#define maxn 22000000char b[maxn+5],a[maxn+5];
int n;
int p[maxn+5];int main() {
scanf("%s",b);n=strlen(b);for(int i=0;i<n;i++) {
a[i*2]='#';a[i*2+1]=b[i];}a[n*2]='#';n*=2;n-=1;int id=0,mx=0;p[0]=1;for(int i=1;i<=n;i++) {
if(i<mx) p[i]=min(p[2*id-i],mx-i);else p[i]=1;while(i-p[i]>=0&&a[i+p[i]]==a[i-p[i]]) p[i]++;if(p[i]+i>mx) {
mx=p[i]+i;id=i;}}int ans=0;for(int i=1;i<=n;i++) {
ans=max(ans,p[i]-1);}printf("%d",ans);return 0;
}