HDU   5371

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  , 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

2015 Multi-University Training Contest 7
 
题意:给一个长为n的序列,求最长连续子序列的长度,这个子序列满足这样的特点:这个子序列由前到后分为长度相等的三部分,第一部分与第二部分对称,第一部分与第三部分相同;
 
思路:先利用回文串算法求出以第i个字符为中心的最大回文长度,然后在这个回文串的最右边的字符出开始判断,如果以这个字符为中心的回文串长度小于前面那个字符的回文串长度,这个比较的字符左移,再次比较,找到以第i个字符为中心的最大长度好,将length个字符遍历一遍就可找到最大的长度。直接这样求解会超时,可以在右边的那个比较的字符向左移动的过程中加上一些判断条件,减少无用的计算。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=;
int p[*N];
int s[*N],str[*N];
int n; void kp()
{
int mx=;
int id;
for(int i=;i<n;i++)
{
if(mx>i)
p[i]=min(p[*id-i],p[id]+id-i);
else
p[i]=;
for( ;i+p[i]<n;)
{
if(str[i+p[i]]==str[i-p[i]])
{
if(str[i+p[i]]==) p[i]++;
else
{
p[i]+=;
}
}
else break;
}
if(p[i]+i>mx)
{
mx=p[i]+i;
id=i;
}
}
} void init()
{
str[]=;
str[]=;
for(int i=; i<n; i++)
{
str[i*+]=s[i];
str[i*+]=;
}
n=n*+;
} int main()
{
int T,Case=;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&s[i]);
if(n<)
{
printf("%d",);
}
init();
kp();
///cout<<p[11]<<"++"<<p[15]<<endl;
int sum=0;
for(int i=;i<=n-;i=i+)
{
if(p[i]-<=sum/*) continue;
int t=p[i];
for(int j=t-;j>=;j=j-)
{
if(j<=sum/*) break;
if(i+j>n) continue;///可能这儿超时;
if(p[i+j]->=j)
{
sum=max(sum,j/*);
break;
}
}
}
printf("Case #%d: %d\n",Case++,sum);
}
return ;
}

回文串---Hotaru's problem的更多相关文章

  1. HDU 5371(2015多校7)-Hotaru&#39;s problem(Manacher算法求回文串)

    题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...

  2. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. POJ 1159 回文串-LCS

    题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文 ...

  4. BZOJ 2342 回文串-Manacher

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2342 思路:先跑一遍Manacher求出p[i]为每个位置为中心的回文半径,因为双倍回文串 ...

  5. BZOJ 2565 回文串-Manacher

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2565 题意:中文题 思路:定义L[i],R[i].表示以i为左端点/右端点时,最长回文串长 ...

  6. POJ 3974 回文串-Manacher

    题目链接:http://poj.org/problem?id=3974 题意:求出给定字符串的最长回文串长度. 思路:裸的Manacher模板题. #include<iostream> # ...

  7. 【BZOJ】3676: [Apio2014]回文串

    http://www.lydsy.com/JudgeOnline/problem.php?id=3676 题意:给一个串求回文串×出现次数的最大值.(|S|<=300000) #include ...

  8. 回文串---Best Reward

    HDU   3613 Description After an uphill battle, General Li won a great victory. Now the head of state ...

  9. 回文串--- Girls' research

    HDU   3294 Problem Description One day, sailormoon girls are so delighted that they intend to resear ...

随机推荐

  1. IBM HTTP Server Performance Tuning

    IBM HTTP Server Performance Tuninghttp://publib.boulder.ibm.com/httpserv/ihsdiag/ihs_performance.htm ...

  2. Mac安装win7

    为了给老板的mac air装上win7,研究了大致4-5小时,终于搞定! 新版的air安装实在是太不容易了,现在记录如下:   制作硬件:8G优盘一个(至少大于6G空白优盘一个),Macbook ai ...

  3. Fedora 手动删除系统中不再需要的包

    最新文章:Virson‘s Blog 1.安装yum-utils yum install yum-utils 2.使用package-cleanup --leaves列举系统中不再需要的包 packa ...

  4. __declspec(dllimport)

    我相信写WIN32程序的人,做过DLL,都会很清楚__declspec(dllexport)的作用,它就是为了省掉在DEF文件中手工定义导出哪些函数的一个方法.当然,如果你的DLL里全是C++的类的话 ...

  5. DotNetBar RibbonControl控件office2007风格

    在使用DotNetBar RibbonControl控件的时候如果想吧效果做成下图这种效果 把主窗体继承Office2007RibbonForm 然后要删除删除styleManager1 才会出现上图 ...

  6. 许可EDM营销是个长期过程

    为什么这么说呢?基于博主自己这三四年的理解,许可EDM营销确实是个长期的过程,这跟一般的EDM营销有一定的区别. 大多数时候不会有立竿见影的效果,而且需要持续地不间断地进行到底,这也是很多企业实施许可 ...

  7. archlinux 下 nignx + php 出现 no input file specified

    奇葩的问题,配置 nginx + php + mysql 后,加一个站点: server { listen 80; server_name wei.abc.com; root /www/wei.abc ...

  8. 使用word和pdf进行仿书编辑的经验

    一.问题的提出:    一本书扫描好,要将书中的图片转换为文字版的word文档.二.问题的分析:    1.文字的提取    2.文字的编排三.问题的解决    1.如果用的是Adobe Acroba ...

  9. c/c++: c++继承 内存分布 虚表 虚指针 (转)

    http://www.cnblogs.com/DylanWind/archive/2009/01/12/1373919.html 前部分原创,转载请注明出处,谢谢! class Base  {  pu ...

  10. [转]make: 警告:检测到时钟错误。您的创建可能是不完整的。

    转自http://blog.csdn.net/maopig/article/details/6599660 我在make的时候也出现了同样的问题,不过不是什么大问题,这个不影响编译结果 分析原因可能是 ...