【题解】HDU5371 Hotaru's proble

Hotaru's problem

题目传送门HDU5371 Hotaru's problem 有时候hdu可能会挂

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

思路

枚举O(n)个回文串中心

找到右边界中心半径大于所枚举的中心半径的所有情况,记最大答案

blabla。。。

代码实现

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=200010;
int n,p[N],ans,len2,res;
int s[N],str[N];

void init()
{
	str[0]=-2;
	str[1]=-1;
	for(int i=0;i<n;i++)
	{
		str[i*2+2]=s[i+1];
		str[i*2+3]=-1;
	}
	len2=2*n+2;
	str[len2]=-1;
}


inline void manacher()
{
	int mid=0,mr=0;
	memset(p,0,sizeof p);
	for(int i=1;i<=len2;i++)
	{
		if(i<mr) p[i]=min(p[mid*2-i],mr-i);
		else p[i]=1;
		while(str[i+p[i]]==str[i-p[i]]) ++p[i];
		if(i+p[i]>mr)
		{
			mr=i+p[i];
			mid=i;
		}	
	
	}
	
}

int main()
{
	int t,tot=0;
	cin>>t;
	while(t--)
	{
		tot++;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&s[i]);
		}
		init();
		manacher();
		
		for(int i=3;i<len2;i+=2)
		{
			for(int j=i+p[i]-1;j-i>ans;j-=2)
			{
				if(j-i+1<=p[j])
				{
					ans=max(ans,j-i);
				
				}
			}
		}
		printf("Case #%d: %d\n",tot,ans/2*3);
		
		ans=0;
	}
	
	
	return 0;
}
posted @   watasky  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示