码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5371(Hotaru's problem-2次回文串)

时间:2015-08-26 22:36:46      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

Hotaru‘s problem

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


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 10技术分享9技术分享技术分享 , 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
 

Author
UESTC
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  5421 5420 5419 5418 5417 
 


先匹配第一个,再暴力找第二个,

注意只找比当前答案优的优化速度





#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (300000+10)
#define Sp_char1 (-1)
#define Sp_char2 (-2)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int a[MAXN],n,r[MAXN];
class manacher
{
public:
	int n;
	int s[MAXN];
	int p[2*MAXN+2];
	void mem(){n=0; MEM(s) MEM(p)}
	int str[MAXN*2+2];
	void work()
	{
		str[0]=Sp_char1;
		Rep(i,n) str[2*i+1]=Sp_char2,str[2*i+2]=s[i]; 
		str[2*n+1]=Sp_char2; str[2*n+2]=-3;
		
		n=2*n+2; MEM(p) 
		int mx=0,id=0;
		For(i,n-1) 
		{
			if (i<mx) p[i]=min(p[2*id-i],mx-i);
			
			while(str[i-p[i]]==str[i+p[i]]) ++p[i];
			if (mx<i+p[i]) //mx为已查明的最右端 
			{
				mx=i+p[i];
				id=i;
			}
		}
	}
}S;

int main()
{
//	freopen("C.in","r",stdin);
	
	int T;cin>>T;
	For(kcase,T) {
		S.mem();
		scanf("%d",&n);
		For(i,n) scanf("%d",&a[i]),S.s[i-1]=a[i];
		S.n=n;
	
		S.work();
		int ans=0;
		for(int i=3;i<S.n;i+=2) 
		{ 
			 for(int j=S.p[i];j>ans;j-=2)
			 {
			 	if (S.p[i+(j-1)]>=j) ans=max(ans,j); 
			 }
		}
		ans=(ans-1)/2*3;
		printf("Case #%d: %d\n",kcase,ans);
	} 
	
	
	return 0;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5371(Hotaru's problem-2次回文串)

标签:

原文地址:http://blog.csdn.net/nike0good/article/details/48009409

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!