codeforces 245H
给出一个字符串,询问任意区间内的回文子串的个数。
#include
#include
#include
#include
#define MAX 5007
using namespace std;
char s[MAX];
int n,q,dp[MAX][MAX];
int isPar[MAX][MAX];
int check ( int l , int r )
{
if ( l == r ) return 1;
if ( s[l] != s[r] ) return 0;
if ( r-l == 1 ) return 1;
if ( isPar[l+1][r-1] ) return 1;
return 0;
}
int main ( )
{
while ( ~scanf ( "%s" , s ) )
{
int n = strlen ( s );
memset ( isPar , 0 , sizeof ( isPar ) );
for ( int i = 0 ; i < n ; i++ )
dp[i][i] = 1;
for ( int i = 0 ; i < n ; i++ )
for ( int j = 0 ; j+i < n ; j++ )
{
int k = j+i;
isPar[j][k] = check ( j , k );
}
for ( int i = 1 ; i < n ; i++ )
{
for ( int j = 0 ; j+i int k = j+i;
dp[j][k] = dp[j][k-1] + dp[j+1][k];
if ( i > 1 )
dp[j][k] -= dp[j+1][k-1];
dp[j][k] += isPar[j][k];
}
}
scanf ( "%d" , &q );
while ( q-- )
{
int l,r;
scanf ( "%d%d" , &l , &r );
l--,r--;
printf ( "%d\n" , dp[l][r] );
}
}
}