2 3 51 52 51 4 51 52 52 51
3 4
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
const int maxn=1e6+100;
int str[maxn];
int temp[maxn<<1];
int Len[maxn<<1];
int t,n;
int init()
{
int i,len=n;
temp[0]=0;
for(int i=1;i<=2*len;i+=2)
{
temp[i]=-1;
temp[i+1]=str[i/2];
}
temp[2*len+1]=-1;
temp[2*len+2]=1;
return 2*n+1;
}
int Manacher()
{
int len=init();
int mx=0,ans=0,po=0;//mx:靠右位置,po:i值
for(int i=1;i<=len;i++)
{
if(mx>i) Len[i]=min(mx-i,Len[2*po-i]);
else Len[i]=1;
while(temp[i-Len[i]]==temp[i+Len[i]])
{
if(Len[i]==1) Len[i]++;
else if(temp[i-Len[i]]==-1&&temp[i-Len[i]+1]<=temp[i-Len[i]+3])
Len[i]++;
else if(temp[i-Len[i]]!=-1&&temp[i-Len[i]]<=temp[i-Len[i]+2])
Len[i]++;
else break;
}
if(Len[i]+i>mx)
{
mx=Len[i]+i;
po=i;
}
ans=max(ans,Len[i]);
}
return ans-1;//Len[i]-1;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
REP(i,n) scanf("%d",&str[i]);
printf("%d\n",Manacher());
}
return 0;
}
HDU 4513 吉哥系列故事——完美队形II(Manacher)
原文:http://blog.csdn.net/u013582254/article/details/44037015