hdu 4516

Give you a "string", allows you to find the longest palindrome string, which palindrome string to be like a mountain shape.
In order not to affect the original string to add large numbers.
To be noted that a small, large on both sides of the intermediate calculation MP, due to the added number of other, so the interval is 2

#include <cstdio>
#include <algorithm>
#include <cstring> 

using namespace std; 

int t,n,a[101010],b[202022],mp[202020],ans;

void manacher(){
	memset(mp,0,sizeof(mp));
	int l = 0;
	b[l++] = 1<<10;
	b[l++] = 1<<9;
	for(int i = 0;i<n;i++){ 
		b[l++] = a[i];
		b[l++] = 1<<9; 
	}
	int id = 0,mx = 0; 
	for(int i = 0;i<l;i++){
		mp[i] = mx>i?min(mp[2*id-i],mx-i):1;
		while(b[i+mp[i]] == b[i-mp[i]]&&b[i+mp[i]]<=b[i+mp[i]-2]) mp[i]++;
		if(i+mp[i]>mx){
			id = i;
			mx = i+mp[i];
		}
	} 
}
int main(){
	scanf("%d",&t);	
	while(t--){
		scanf("%d",&n);
		for(int i = 0;i<n;i++)	scanf("%d",a+i);
		manacher();
		ans = 0;
		for(int i = 0;i<2*n+2;i++)
			ans = max(ans,mp[i]-1);
		printf("%d\n",ans);
	} 
	return 0;
}

Guess you like

Origin blog.csdn.net/winhcc/article/details/89681516