pid=5371">http://acm.hdu.edu.cn/showproblem.php?

pid=5371

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

/**
hdu5371 最长回文子串变形(Manacher算法)
题目大意:找出一个字符串能够均分为三段,第一段和第三段同样。和第二段互为回文串
解题思路:利用Manacher算出每一个位置的最长回文子串的长度,然后枚举就可以
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=210001;
int s[maxn],a[maxn];
int r[maxn],len; void Manancher()
{
int l=0;
a[l++]=-2;
a[l++]=-1;
for(int i=0;i<len;i++)
{
a[l++]=s[i];
a[l++]=-1;
}
a[l]=-3;
int mx=0,id=0;
for(int i=0;i<l;i++)
{
r[i]=mx>i? min(r[2*id-i],mx-i):1;
while(a[i+r[i]]==a[i-r[i]])r[i]++;
if(i+r[i]>mx)
{
mx=i+r[i];
id=i;
}
//printf("%d ",r[i]);
}
// printf("\n");
} int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%d",&len);
for(int i=0;i<len;i++)
{
scanf("%d",&s[i]);
}
Manancher();
int ans=0;
for(int i=1;i<=len*2+1;i+=2)
{
for(int j=i+r[i]-1;j-i>ans;j-=2)
{
if(j-i+1<=r[j])
{
ans=max(ans,j-i);
break;
}
}
}
printf("Case #%d: %d\n",++tt,ans/2*3);
}
return 0;
}

hdu5371 最长回文子串变形(Manacher算法)的更多相关文章

  1. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  2. 最长回文子串的Manacher算法

    对于一个比较长的字符串,O(n^2)的时间复杂度是难以接受的.Can we do better? 先来看看解法2存在的缺陷. 1) 由于回文串长度的奇偶性造成了不同性质的对称轴位置,解法2要对两种情况 ...

  3. 51nod1089(最长回文子串之manacher算法)

    题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 题意:中文题诶~ 思路: 我前面做的那道回文子串的题 ...

  4. 求最长回文子串:Manacher算法

    主要学习自:http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html 问题描述:回文字符串就是左右 ...

  5. 最长回文子串(Manacher算法)

    回文字符串,想必大家不会不熟悉吧? 回文串会求的吧?暴力一遍O(n^2)很简单,但当字符长度很长时便会TLE,简单,hash+二分搞定,其复杂度约为O(nlogn), 而Manacher算法能够在线性 ...

  6. 计算字符串的最长回文子串 :Manacher算法介绍

    转自: http://www.open-open.com/lib/view/open1419150233417.html Manacher算法 在介绍算法之前,首先介绍一下什么是回文串,所谓回文串,简 ...

  7. hihocoder #1032 : 最长回文子串【 manacher算法实现 】

    #1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...

  8. 51Nod 1089 最长回文子串 V2 —— Manacher算法

    题目链接:https://vjudge.net/problem/51Nod-1089 1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值:  ...

  9. 51 Nod 1089 最长回文子串(Manacher算法)

    1089 最长回文子串 V2(Manacher算法)  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 回文串是指aba.abba.cccbccc.aaa ...

随机推荐

  1. Acunetix Web Vulnerability Scanner使用和生成报告的方法

    Acunetix WVS,该扫描软件的全称Acunetix Web Vulnerability Scanner,是一个网站及服务器漏洞扫描软件.它可以检查Web应用程序中的漏洞,如SQL注入.跨站脚本 ...

  2. jdbc 实现分页

    jdbc 实现分页,的实现 原理这个就不介绍了.. 总之是用jdbc 的游标移动 package com.sp.person.sql.util; import java.sql.Connection; ...

  3. HDU_3182_Hamburger Magi_状态压缩dp

    Hamburger Magi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. MySQL(四)DQL语言——条件查询

    摘要:条件查询:条件表达式,逻辑表达式,模糊查询,like,通配符,转义字符,escape关键字,between and,in,is null,is not null,安全等于. 条件查询语法: SE ...

  5. TCP端口状态LISTENING ESTABLISHED CLOSE_WAIT TIME_WAIT SYN_SENT

    TCP状态转移要点 TCP协议规定,对于已经建立的连接,网络双方要进行四次握手才能成功断开连接,如果缺少了其中某个步骤,将会使连接处于假死状态,连接本身占用的资源不 会被释放.网络服务器程序要同时管理 ...

  6. 梦想CAD控件网页版扩展数据

    随着基于CAD的应用软件飞速发展,经常需要保存一些与图形可视性无关的数据,即非图形参数.例如在绘制化验样图中包含品位数据.MxCAD定义一类新的参数——实体扩展数据.扩展数据与实体的可视性无关,而是用 ...

  7. JavaScipt30(第八个案例)(主要知识点:canvas)

    承接上文,这是第8个案例,要实现的效果是按住鼠标不放,进行拖动时可以在画布上画出不同粗细不同颜色的曲线. 附上项目链接: https://github.com/wesbos/JavaScript30 ...

  8. CentOS 7中firewall防火墙详解和配置以及切换为iptables防火墙--转载

    最近在linux(这里用到的是Centos7的64位版本)安装nginx时,在开放80端口时用iptables设置端口 和重启服务发现提示未找到文件,在网络上收集查找后发现在Centos7中iptab ...

  9. 队列的头函数使用C++

    queue queue模板类的定义在<queue>头文件中. 与stack模板类很相似,queue模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的 ...

  10. MyBatis 的基本要素—SQL 映射文件

    MyBatis 真正的强大在于映射语句,相对于它强大的功能,SQL 映射文件的配置却是相当简单.对比 SQL 映射配置和 JDBC 代码,发现使用 SQL 映射文件配置可减少 50% 以上的代码,并且 ...