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. Javascript的对象分类

    返回索引 按W3CSchool分类 1.JS内置对象 在W3CShool中对应JavaScript对象  参考

  2. Vue 组件 生命周期函数mounted

    生命周期函数mounted:页面刷新触发mounted(){ console.log('我在页面刷新时触发');} Tips:使用export default function Add(){},与ex ...

  3. D3.js 之 d3-shap 简介(转)

    [转] D3.js 之 d3-shap 简介 译者注 原文: 来自 D3.js 作者 Mike Bostock 的 Introducing d3-shape 译者: ssthouse 联系译者: 邮箱 ...

  4. Swift3.0 UICollectionView简单使用

    感觉swift各版本语法改动太大,储备着吧

  5. jest+vue-test-utils初步实践

    一.起步 1. jest Jest是 Facebook 的一套开源的 JavaScript 测试框架, 它自动集成了断言.JSDom.覆盖率报告等开发者所需要的所有测试工具,配置较少,对vue框架友好 ...

  6. Dynamic Rankings(整体二分)

    Dynamic Rankings(整体二分) 带修区间第k小.\(n,q\le 10^4\). 这次我们旧瓶装新酒,不用带修主席树.我们用整体二分!整体二分是啥东西呢? 二分答案可以解决一次询问的问题 ...

  7. html二

    超链接 超链接有三种形式: 1.外部链接:链接到外部文件.举例: <a href="new.html">点击进入到新网页</a> a是英语anchor“锚” ...

  8. 最短路【bzoj2464】: 中山市选[2009]小明的游戏

    2464: 中山市选[2009]小明的游戏 Description 小明最近喜欢玩一个游戏.给定一个n * m的棋盘,上面有两种格子#和@.游戏的规则很简单:给定一个起始位置和一个目标位置,小明每一步 ...

  9. arcgis图片文件

  10. CF352B Jeff and Periods 模拟

    One day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decide ...