吉哥系列故事--完美队形 - HDU 4513 (Manacher)

分析:刚开始的想法是匹配出来每个点的最大回文串,然后用每个点的最大不下降序找一个最小值,不过不明白为什么不对....很无奈,再匹配的时候加一些判断就可以过,或许还有什么没想到的地方吧.....
 
代码如下:
=====================================================================================================================
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int MAXN = 2e5+7;
const int oo = 1e9+37;

int Estr[MAXN], str[MAXN], p[MAXN];

int Manacher(int len)
{
    int id=0, Max=0;

    for(int i=2; i<len; i++)
    {
        p[i] = 1;

        if(p[id]+id > i)
            p[i] = min(p[id*2-i], p[id]+id-i);

        while(Estr[i+p[i]] == Estr[i-p[i]] && (Estr[i-p[i]]==-2 || p[i]==1 || Estr[i-p[i]]<=Estr[i-p[i]+2]))
            p[i]++;

        if(p[id]+id < p[i]+i)
            id = i;

        Max = max(p[i]-1, Max);
    }

    return Max;
}

int main()
{
    int T;

    scanf("%d", &T);

    while(T--)
    {
        int i, N;

        scanf("%d", &N);

        for(i=0; i<N; i++)
        {
            scanf("%d", &str[i]);
            Estr[i+i+2] = str[i];
            Estr[i+i+1] = -2;
        }
        Estr[0] = oo;
        Estr[N+N+1] = -2;
        Estr[N+N+2] = -oo;

        printf("%d\n", Manacher(N+N+1));
    }

    return 0;
}

 

posted @ 2015-08-22 09:13  无忧望月  阅读(154)  评论(0编辑  收藏  举报
levels of contents