hdu5371 Hotaru's problem(Manacher算法变形)

181 篇文章 0 订阅
171 篇文章 0 订阅

Link:http://acm.hdu.edu.cn/showproblem.php?pid=5371


Hotaru's problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1683    Accepted Submission(s): 605


Problem Description
Hotaru Ichijou recently is addicated to math problems. Now she is playing with N-sequence.
Let's define N-sequence, which is composed with three parts and satisfied with the following condition:
1. the first part is the same as the thrid part,
2. the first part and the second part are symmetrical.
for example, the sequence 2,3,4,4,3,2,2,3,4 is a N-sequence, which the first part 2,3,4 is the same as the thrid part 2,3,4, the first part 2,3,4 and the second part 4,3,2 are symmetrical.

Give you n positive intergers, your task is to find the largest continuous sub-sequence, which is N-sequence.
 

Input
There are multiple test cases. The first line of input contains an integer T(T<=20), indicating the number of test cases. 

For each test case:

the first line of input contains a positive integer N(1<=N<=100000), the length of a given sequence

the second line includes N non-negative integers ,each interger is no larger than  109  , descripting a sequence.
 

Output
Each case contains only one line. Each line should start with “Case #i: ”,with i implying the case number, followed by a integer, the largest length of N-sequence.

We guarantee that the sum of all answers is less than 800000.
 

Sample Input
  
  
1 10 2 3 4 4 3 2 2 3 4 4
 

Sample Output
  
  
Case #1: 9
 

Source
 

AC code:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#define LL long long
#define MAXN 100005
using namespace std;
int s1[MAXN*2],s2[MAXN*2];//s1为原字符串,s2为转换后的字符串,注意这里要改成int,char会TLE!!! 
int p[MAXN*2];//p[i]表示以s2[i]为中心(包括s2[i])的回文半径,对应s1的回文长度 
int len,n; 
int a[MAXN*2];
int l,r;//最长回文子串对应原串s1中的位置:l = (i - p[i])/2; r = (i + p[i])/2 - 2;
void pre()
{
	int i,j,k;
	//len=strlen(s1);
	len=n;
	s2[0]=-2;
	s2[1]=-1;
	for(i=0;i<len;i++)
	{
		s2[i*2+2]=s1[i];
		s2[i*2+3]=-1;
	}
	len=len*2+2;
	s2[len]=0;
}
void Manacher()
{
	int i;
	int mx=0;
	int id;
	int ans=0;
	for(i=1;i<len;i++)
	{
		if(mx>i)
			p[i]=min(p[2*id-i],mx-i);
		else
			p[i]=1;
		for(;s2[i+p[i]]==s2[i-p[i]];p[i]++);
		if(ans<p[i])
		{
			ans=p[i];
		//	l = (i - p[i])/2;//最长回文子串对应原串s1中的左位置 
		//	r = (i + p[i])/2 - 2;//最长回文子串对应原串s1中的右位置
		}
		//ans=max(ans,p[i]);
		if(p[i]+i>mx)
		{
			mx=p[i]+i;
			id=i;
		}
	}
	//return ans-1;//返回最长回文子串长度 
}
int main()
{
	int t,i,j,k,an;
	scanf("%d",&t);
	int cas=0;
	while(t--)
	{
		cas++;
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			s1[i]=a[i];
		}
		memset(p,0,sizeof(p));
		pre();
		Manacher();
		an=0;
		for(i=3;i<len;i+=2)
		{
			if(p[i]-1>an)
			{
				int rr=p[i]-1;
				while(rr>an&&p[i+rr]<rr)
					rr--;
				an=max(an,rr);
			}
		}
		printf("Case #%d: %d\n",cas,an/2*3);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值