hdu 4513

题意:最长回文子串满足从左到中单调非减。

思路:改manacher的扩展条件,使得只有单调递增的时候进行扩展。

#include 
using namespace std;
const int maxn = 200005;
const int inf= 0x3f3f3f3f;
void manacher( const int* str,int* r,int n ){
    int mx(0),c(1),i,j;
    int len = n;
    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] && (( str[i+j] == inf ) || (str[i+j] <= str[i+j-2]) ) )++j;
        if( i+j-1 > mx ){
            mx = i+j-1;
            c = i;
        }
        r[i] = j;
    }
}
int str[maxn],r[maxn],ss[maxn];
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        for( int i = 0;i < n;i++ )scanf("%d",&ss[i]);
        for( int i = 0;i < n;i++ ){
            str[i<<1] = inf;
            str[i<<1|1] = ss[i];
        }
        str[n<<1] = inf;
        manacher(str,r,(n<<1)+1);
        int ans = 0;
        for( int i = 0;i < 2*n;i++ ){
            int cur = (r[i]*2-1)>>1;
            ans = max( ans,cur );
        }
        printf("%d\n",ans);
    }
    return 0;
}
 
 

 

你可能感兴趣的:(manacher)