malacher裸题
注意malacher的数组应该开二倍。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
void manacher( const char* str,int* r ){
int mx(0),c(1),i,j;
int len = strlen(str);
r[0] = r[len-1] = 1;
for( i = 1;i < len-1;i++ ){
if( mx >= i ) j = min( r[ (c<<1)-i ],mx-i+1 );
else j = 1;
while((i-j) >= 0 && (i+j) < len && str[i-j] == str[i+j] )++j;
if( i+j-1 > mx ){
mx = i+j-1;
c = i;
}
r[i] = j;
}
}
char ss[maxn],str[maxn];
int r[maxn],le[maxn],ri[maxn];
int main(){
scanf("%s",ss);
int len = strlen(ss);
for( int i = 0;i < len;i++ ){
str[i<<1] = '#';
str[i<<1|1] = ss[i];
}
str[2*len] = '\0';
manacher( str,r );
int mx = -1;
for( int i = 0;i < 2*len;i++ ){
if( r[i] + i - 1 > mx ){
for( int j = mx+1;j <= r[i]+i-1;j++ ){
if(j&1)
le[j/2] = (2*i-j)/2;
}
mx = r[i] + i - 1;
}
}
int mn = len*2;
for( int i = 2*len-1;i >= 0;i-- ){
if( i-r[i]+1 <mn ){
for( int j = mn-1;j >= i-r[i]+1;j-- ){
if( j&1)
ri[j/2] = (2*i-j)/2;
}
mn = i-r[i]+1;
}
}
int ans = 2;
for( int i = 0;i < len-1;i++ ){
ans = max( ans,i-le[i]+1+ri[i+1] - i);
}
printf("%d\n",ans);
return 0;
}