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

题目链接:点击查看

题目大意:给出一个长度为n的数列,现在要求选出一段连续的数列,满足:

  1. 该数列为回文串
  2. 该数列的左半部分非严格递增

输出选取数列的最大长度

题目分析:因为是要选取连续的子串,并且还需要输出最大长度,不难想到要用马拉车变形解决,条件中只是多了一个,需要让左半部分严格递增的要求,那么我们可以在马拉车模板的基础上稍作修改就是本题的答案了

代码:

#include
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

typedef long long LL;

const int inf=0x3f3f3f3f;

const int N=2e5+100;

int a[N],p[N],n;

int Manacher()
{
	p[0]=1;
	int id=0,mmax=0,ans=0;
	for(int i=1;immax)
		{
			mmax=i+p[i];
			id=i;
		}
		ans=max(ans,p[i]-1);
	}
	return ans;
}

int main()
{
//	freopen("input.txt","r",stdin);
//	ios::sync_with_stdio(false);
	int w;
	cin>>w;
	while(w--)
	{
		int nn;
		scanf("%d",&nn);
		n=0;
		a[n++]=inf;
		while(nn--)
		{
			a[n++]=0;
			scanf("%d",&a[n++]);
		}
		a[n++]=0;
		a[n++]=-inf;
		printf("%d\n",Manacher());
	}
	
	
	
	
	
	
	
	
	
	
	
	return 0;
}

 

你可能感兴趣的:(字符串处理,马拉车)