Hotaru’s problem

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

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


解题心得:

  1. 就是manacher的应用,要求的是两个回文串,左方回文串左半和右方回文串右半相重合。所以要使用manacher求出以每个数字为对称轴的半径。然后在判断是否两个回文串符合要求。
  2. 关于判断重和部分,先找到一个对称轴然后每次从ans(之取最大值)到当前部分对称半径枚举,找到右半部份的对称中心(左半部分的对称轴+左半部分的对称半径),如果右半部份的对称半径符合要求则改变ans(因为是从ans开始,所以得到的答案肯定大于ans)。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+100;
int num[maxn],rl[maxn]; //manacher模板
void manacher(int tot)
{
int max_right = 0,pos = 0;
for(int i=0;i<tot;i++)
{
if(i < max_right)
rl[i] = min(rl[pos*2-i],max_right-i);
else
rl[i] = 1;
while(i-rl[i]>=0 && i+rl[i]<tot && num[i+rl[i]] == num[i-rl[i]])
rl[i]++;//找到以每个数字为对称中心的对称半径
if(i+rl[i]-1 > max_right)
{
max_right = i+rl[i]-1;
pos = i;
}
}
} int main()
{
int t,T;
scanf("%d",&t);
T = t;
while(t--)
{
int n;
scanf("%d",&n);
int tot = 0;
for(int i=0;i<n;i++)
{
int now;
scanf("%d",&now);
num[tot++] = -1;
num[tot++] = now;
}
num[tot++] = -1;
manacher(tot);
int ans = 1;
for(int i=0;i<tot;i+=2)//从题目可以看出肯定是偶数长度的回文串
{
for(int j=ans;j<=rl[i];j+=2)
if(rl[i+j-1]>=j)//左右半径可以相互重合
ans = j;
}
printf("Case #%d: %d\n",T-t,ans/2*3);//ans/2*3的出的才是两个符合要求的回文串的长度
}
}

字符串:HDU5371-Hotaru's problem(manacher 的应用)的更多相关文章

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

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

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

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

  3. HDU5371 Hotaru's problem

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. hdu5371 Hotaru's problem

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

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

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

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

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

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

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

  8. Manacher HDOJ 5371 Hotaru's problem

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

  9. Hotaru's problem

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

随机推荐

  1. Python踩坑之旅其二裸用os.system的原罪

    目录 1.1 踩坑案例 1.2 填坑解法 1.3 坑位分析 1.4.1 技术关键字 1.5 填坑总结 2. 前坑回顾 2.1 Linux中, 子进程拷贝父进程哪些信息 2.2 Agent常驻进程选择& ...

  2. ruby 从命令行读取文本

    最常见的方式就是使用内置的get 方法,这个方法可以从命令行读取用户的输入,并在默认的情况下把读入的文本赋值给预定义变量$_. 但是get方法会保留用户在输入字符串末尾所加的换行符,当用户在输入的字符 ...

  3. 面向切面编程 (AOP )的理解

    AOP的全称: Aspact  Oriented  Programming AOP的目标(作用):让我们可以“专心做事”  日志记录,事务处理,异常捕获,缓存操作. AOP原理 将复杂的需求分解出不同 ...

  4. vue学习之路之需要了解的知识汇总

    一.vue是什么? 相关网页:  https://vuejs.bootcss.com/v2/guide/       及菜鸟教程       https://www.runoob.com/vue2/v ...

  5. LNK2005错误——重复定义错误

    编程中经常能遇到LNK2005错误——重复定义错误,其实LNK2005错误并不是一个很难解决的错误.弄清楚它形成的原因,就可以轻松解决它了. 造成LNK2005错误主要有以下几种情况: 1.重复定义全 ...

  6. Object-C反射读取实体属性和值

    举例: 首先定义TestModel如下: @interface TestModel : NSObject @property (nonatomic, strong) NSString *name; @ ...

  7. 常用浏览器User-Agent大全

    =======================PC浏览器======================== OperaMozilla/5.0 (Windows NT 6.1; WOW64) AppleW ...

  8. ASP.NET WebForm & MongoDB

    ASP.NET WebForm & MongoDB 最近在朋友介绍下,也跟着看AngularJS 买了一本三合一的书,Node.JS+MongoDB+AngularJS http://www. ...

  9. Linux 的数字权限意义

    三个组 每个都有三个权限 r w x每个权限用二进制 0 和 1 标示 1即为有此权限 0 标示无权限  ower    group  other  r w x    r w x  r w x 每个组 ...

  10. 关于日志造成的频繁的IO

    记录日志可能消耗大量的IO [Q] 每次写入都是一个IO操作 即使是同一个文件 两次写入也要打开两次IO操作 [F] 设想有这样一个扩展  把php中要记录的日志 用文件名 和 内容的方式记录在内存中 ...