BZOJ 2565: 最长双回文串

时间:2023-01-08 09:33:48

智商康复计划。。。。。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define per(i,r,l) for(int i=r;i>=l;i--)
#define mmt(a,v) memset(a,v,sizeof(a))
const int N=100000+5;
char s[N<<1],t[N];
int n;
int r[N<<1],a[N<<1],b[N<<1];
int manacher(){
	int m=n<<1|1;
	rep(i,1,m)if(i&1)s[i]='#';
	else s[i]=t[i>>1];
	s[m]='#';
	int id=0,mx=0;
	rep(i,1,m){
		if(mx>i)r[i]=min(mx-i,r[2*id-i]);
		else r[i]=1;
		while(i-r[i]>=1&&i+r[i]<=m&&s[i-r[i]]==s[i+r[i]])r[i]++;
		if(i+r[i]>mx)mx=i+r[i],id=i;
	}
	int j=1;
	rep(i,1,m){
		while(j+r[j]<=i)j++;
		a[i]=j;
	}
	j=m;
	per(i,m,1){
		while(j-r[j]>=i)j--;
		b[i]=j;
	}
	int ans=0;
	rep(i,1,m)if(i&1)
	ans=max(ans,b[i]-a[i]);
	return ans;
}
int main(){
	//freopen("a.in","r",stdin);
	scanf("%s",t+1);
	n=strlen(t+1);
	printf("%d\n",manacher());
	return 0;
}