本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

 
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
 
 
正解:manacher
解题报告:
  求形如ABA(B与A对称0)的串的最大长度。
  先用manacher预处理出以每个点为中心的最长回文子串长度,枚举第二个部分即B的起点,再枚举串的长度,发现可行则更新答案(代码中非常清楚了)。可以用已经得到的ans剪枝。
  辣鸡HDU评测速度飘忽不定,刷了几次才跑过。
 
 
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 200011;
int n,len,a[MAXN],p[MAXN],ans;
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void manacher(){
//memset(p,0,sizeof(p));
for(int i=1;i<=n;i++) p[i]=0;
int maxR=0,id=0;
for(int i=1;i<=n;i++) {
if(i<maxR) p[i]=min(p[2*id-i],maxR-i);
else p[i]=1;
for(;i+p[i]<=n && a[i-p[i]]==a[i+p[i]];p[i]++) ;
if(i+p[i]>maxR) { maxR=i+p[i]; id=i; }
}
} inline void work(){
int T=getint(); int Case=0;
while(T--) {
len=getint(); a[0]=-2; a[1]=-1; n=1; for(int i=1;i<=len;i++) { a[++n]=getint(); a[++n]=-1; }
manacher(); ans=1;
//+2的原因是对称中心只能取在区分符号上
for(int i=3;i<n/*!!!*/;i+=2) //枚举the second part
for(int j=ans;j<=p[i];j+=2) //枚举一个部分的长度,利用ans剪枝
if(p[i+j-1]>=j) //如果the third part的回文长度大于等于j,即j可以作为答案(part2和part3对称)
ans=j;
Case++; ans/=2; ans*=3;//只算了两个part的长度
printf("Case #%d: %d\n",Case,ans);
}
} int main()
{
work();
return 0;
}

  

HDU5371 Hotaru's problem的更多相关文章

  1. [2015hdu多校联赛补题]hdu5371 Hotaru's problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...

  2. hdu5371 Hotaru's problem

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

  3. [hdu5371 Hotaru's problem]最大回文半径

    题意:在一个字符串里面找最长的[A][B][A]子串,其中[A][B]是回文串,[A]和[B]的长度相等 思路:[A][B]是回文串,所以[B][A]也是回文串.先预处理出每个点的最大回文半径Ri,枚 ...

  4. Hotaru's problem

    Hotaru's problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. HDU 5371——Hotaru's problem——————【manacher处理回文】

    Hotaru's problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. Hdu 5371 Hotaru's problem (manacher+枚举)

    题目链接: Hdu 5371 Hotaru's problem 题目描述: 给出一个字符串N,要求找出一条N的最长连续子串.这个子串要满足:1:可以平均分成三段,2:第一段和第三段相等,3:第一段和第 ...

  7. Manacher HDOJ 5371 Hotaru's problem

    题目传送门 /* 题意:求形如(2 3 4) (4 3 2) (2 3 4)的最长长度,即两个重叠一半的回文串 Manacher:比赛看到这题还以为套个模板就行了,因为BC上有道类似的题,自己又学过M ...

  8. 2015 Multi-University Training Contest 7 hdu 5371 Hotaru's problem

    Hotaru's problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. hdu5371 Hotaru&#39;s problem

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

随机推荐

  1. 图解WinHex使用入门

    一 Winhex和相关概念简单介绍 1 Winhex 是在Windows下执行的十六进制编辑软件,此软件功能很强大,有完好的分区管理功能和文件管理功能.能自己主动分析分区链和文件簇链.能对硬盘进行不同 ...

  2. Atitit.业务系统的新特性 开发平台 新特性的来源总结

    Atitit.业务系统的新特性 开发平台 新特性的来源总结 1.1. 语言新特性(java c# php js python lisp c++ oc swift ruby  go dart1 1.2. ...

  3. 配置远程maven仓库自己的步骤

    ---恢复内容开始--- 1.首先在远程服务器配置jdk.maven环境,这个可以在网上找 2.Nexus 安装与配置.这个见网上博客(https://blog.csdn.net/lewis_007/ ...

  4. 禁止"Windows Media Player Network Sharing Service"服务自动启动

    开始 -> 运行 -> gpedit.msc -> 计算机配置 -> 管理模板 -> Windows 组件 -> Windows Media Player -> ...

  5. Photoshop中磁力套索的一种简陋实现(基于Python)

    经常用Photoshop的人应该熟悉磁力套索(Magnetic Lasso)这个功能,就是人为引导下的抠图辅助工具.在研发领域一般不这么叫,通常管这种边缘提取的办法叫Intelligent Sciss ...

  6. xml初学简单介绍

    什么是XML? 1.全称Extensible Markup Language,可扩展标记语言,W3C组织公布. 2.XML用来保存有一定结构关系的数据. 3.标签的嵌套,实质是一串字符串. 4.跨平台 ...

  7. Java NIO —— Buffer(缓冲区)

    Buffer是一个抽象类,位于java.nio包中,主要用作缓冲区.注意:Buffer是非线程安全类. 缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这块内存被包装成NIO Buffer ...

  8. android中实现毛笔效果(View 中画图)

    近期有一个项目设计一个APP实现通过触摸屏实现毛笔写字效果.传统的绘画板程序直接通过Path的moveTo和LineTo便可实现简单的线条绘画程序.然而要达到毛笔的笔锋效果则须要更为具体点的设计.我的 ...

  9. 微信小程序的文件结构 —— 微信小程序教程系列(1)

    所有文章均是CSDN博客所看,已按照作者要求,注明出处了,感谢作者的整理! 博客文章地址:http://blog.csdn.net/michael_ouyang/article/details/548 ...

  10. PHP-Manual的学习----【语言参考】----【类型】-----【Boolean类型】

    2017年7月20日15:41:26Boolean 布尔类型 1.这是最简单的类型.boolean 表达了真值,可以为 TRUE 或 FALSE. 其实就是真假的问题.2.语法 要指定一个布尔值,使用 ...