Hotaru's problem

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

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
 
题目大意:给你t组数据,一个n,下面一行有n个数,问你能形成形如abccbaabc这样的序列长度最长是多少。
 
解题思路:先用manacher处理出来串的所有字符的回文半径。然后枚举第一段跟第二段回文位置i,从i+p[i]-->i进行枚举第二段跟第三段回文位置j。如果以j为回文中心的左端能小于等于i的位置,说明满足要求,更新结果。
 
#include<bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
const int maxn=1e6;
int a[maxn],p[maxn];
void Manacher(int n){
a[0]=-2;a[n+1]=-1;a[n+2]=-3;
int mx=0,id=0;
for(int i=1;i<=n+1;i++){ //需要处理到n+1
if(i<mx){
p[i]=min(p[id*2-i],mx-i); //这里写的时候写成mx-id,SB了。
}else{
p[i]=1;
}
for(;a[i+p[i]]==a[i-p[i]];++p[i]); //-2,-3防越界
if(i+p[i]>mx){
mx=p[i]+i;
id=i;
}
}
for(int i=1;i<=n+1;++i){
--p[i];
}
}
int main(){
// freopen("1003.in","r",stdin);
// freopen("OUTTTT.txt","w",stdout);
int t,n,cnt=0;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=2*n;i+=2){
a[i]=-1;
scanf("%d",&a[i+1]);
} Manacher(2*n);
int maxv=0,j;
for(int i=1;i<=2*n;i+=2){ //2*n
for(j=i+p[i];j-maxv>i;j-=2){ //逆序枚举。manacher算法保证j<=2*n+1
if(j-p[j]<=i){
maxv=j-i;
break;
}
}
}
maxv=3*(maxv/2);
printf("Case #%d: %d\n",++cnt,maxv);
}
return 0;
}

  

HDU 5371——Hotaru's problem——————【manacher处理回文】的更多相关文章

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

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

  2. HDU 5371 Hotaru's problem Manacher+尺取法

    题意:给你一个序列,求最长的两段回文子串,要求他们共用中间的一半. 思路:利用Manacher求出p[i]表示的当前位置的最长回文串长度,然后把每一个长度大于等于2的回文串的左区间和右区间分别放到两个 ...

  3. HDU 5371 Hotaru's problem (Manacher,回文串)

    题意:给一个序列,找出1个连续子序列,将其平分成前,中,后等长的3段子序列,要求[前]和[中]是回文,[中]和[后]是回文.求3段最长为多少?由于平分的关系,所以答案应该是3的倍数. 思路:先Mana ...

  4. 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) ...

  5. HDU 5340——Three Palindromes——————【manacher处理回文串】

    Three Palindromes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. Manacher HDOJ 5371 Hotaru's problem

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

  7. Manacher以及回文树算法学习

    Manacher以及回文树算法学习 一.Manacher 关于\(Manacher\),这篇博客 讲的很清楚. 大致总结一下 为了将长度为奇数的回文串和长度为偶数的回文串一起考虑,需要在原字符串中插入 ...

  8. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

  9. hdu 5371 Hotaru&#39;s problem【manacher】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=5371 题意: 给出一个长度为n的串,要求找出一条最长连续子串.这个子串要满足:1:能够平均分成三段 ...

随机推荐

  1. C#:数据库通用访问类 SqlHelper

    using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; usin ...

  2. UC浏览器体验

    1.用户界面: 有两个页面,一个展示网页应用-可添加自己喜欢的网页应用,另一个用来搜索,有推荐的常用的网址,有UC头条,页面下有设置,整体布局常规 2.短期刺激: 没有特别花哨的地方:个人感觉比较实用 ...

  3. 继承&多态

    继承: 概念: 基类,超累,父类 访问权限: Public :无限制,自由访问 Private:不可继承 protected :包内,包外,可访问,继承 default:没有指明任何权限下,默认在同一 ...

  4. Binder学习笔记(五)—— Parcel是怎么打包数据的?

    前文中曾经遇到过Parcel,从命名上知道他负责数据打包.在checkService的请求/响应体系中,Parcel只打包了基本数据类型,如Int32.String16……后面还要用于打包抽象数据类型 ...

  5. (Delphi)第一个Windows 32 API的窗口程序

    program Project1; uses Winapi.Windows, Winapi.messages; {$R *.res} const className = 'MyDelphiWindow ...

  6. 787. Cheapest Flights Within K Stops

    There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...

  7. GET/POST/g和钩子函数(hook)

    GET请求和POST请求: 1. get请求: * 使用场景:如果只对服务器获取数据,并没有对服务器产生任何影响,那么这时候使用get请求. * 传参:get请求传参是放在url中,并且是通过`?`的 ...

  8. .NET 反射

    反射是.NET很强大的一个机制. 它就像照妖镜一般的存在.它能调用你的任意私有成员,如:私有构造函数.私有方法.私有字段. 类的构造函数声明为了private,别人无法实例化对象出来?No,No,No ...

  9. 小程序不在以下 request 合法域名列表中

    我们如果是正式上线可以在小程序后台配置合法域名,必须是https 测试时我们可以关闭验证 在 设置 - 项目设置 里勾选不校验https 和 TLS

  10. [JavaScript]Prototype继承

    JavaScript相对于其他的编程语言是比较简单的,只要吃透了Prototype和Closure(闭包),基本上就可以说精通JavaScript了. JavaScript里如何实现向Java语言的O ...