不知道错在哪了,求大神指教!!!
思路:用manacher求出每个以str[i]为中心轴的回文串的长度,RMQ预处理区间最大值,对于每个查询,二分最大回文串长,判定是否可行。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int MAXN = ; char str[ MAXN ];
int data[ MAXN * ];
int p[ MAXN * ];
int d[ MAXN * ][];
int n, len; void init()
{
int id,MaxL,MaxId;
int i;
MaxL=MaxId=;
len = strlen(&str[]);
for (i=; i <= len; i++)
{
data[(i<<)]=str[i];
data[(i<<)+]=;
}
data[]=;
data[]=;
n=(i<<)+;
data[n]=;
MaxId=MaxL=;
p[] = ;
for (i=; i<n; i++)
{
if (MaxId>i)
p[i]=min(p[*id-i],MaxId-i);
else
p[i]=;
while (data[i+p[i]] == data[i-p[i]])
p[i]++;
if (p[i]+i>MaxId)
MaxId=p[i]+i,id=i;
//printf( "p[%d]=%d\n", i, p[i] );
}
for ( i = ; i < n; ++i ) --p[i]; return;
} void RMQinit()
{
for ( int i = ; i <= n; ++i ) d[i][] = p[i];
for ( int j = ; ( << j ) <= n; ++j )
for ( int i = ; i + j - <= n; ++i )
d[i][j] = max( d[i][j - ], d[ i + (<<(j-))][j - ] );
return;
} int RMQquery( int L, int R )
{
int k = ;
while ( ( << (k + )) <= R - L + ) ++k;
return max( d[L][k], d[ R - ( << k ) + ][k] );
} bool check( int L, int R, int mid )
{
//if ( L + mid > R - mid ) return false;
int ans = RMQquery( L + mid, R - mid );
//printf("[%d %d]:%d\n", L + mid, R - mid, ans );
//printf("ans = %d, mid = %d\n", ans, mid );
if ( ans >= mid )
{
//puts("****");
return true;
}
return false;
} int BiSearch( int l, int r, int L, int R )
{
int ans = ;
while ( l <= r )
{
int mid = ( l + r ) >> ;
if ( check( L, R, mid ) )
{
l = mid + ;
ans = mid;
}
else r = mid - ;
}
return ans;
} int main()
{
int T;
scanf( "%d", &T );
getchar();
while ( T-- )
{
memset( d, , sizeof(d) );
memset( p, , sizeof(p) );
gets( &str[] );
init();
RMQinit(); int Q;
scanf( "%d", &Q );
while ( Q-- )
{
int a, b;
scanf( "%d%d", &a, &b );
if ( a < ) a = ;
if ( b > len ) b = len;
if ( a > len )
{
puts("");
continue;
}
if ( a > b ) swap( a, b );
int ans = BiSearch( , b - a + , a* - , b* + );
printf( "%d\n", ans );
}
getchar();
}
return ;
}