hdu5371 最长回文子串变形(Manacher算法)
pid=5371">http://acm.hdu.edu.cn/showproblem.php? pid=5371
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.
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.
We guarantee that the sum of all answers is less than 800000.
1
10
2 3 4 4 3 2 2 3 4 4
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算法)的更多相关文章
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- 最长回文子串的Manacher算法
对于一个比较长的字符串,O(n^2)的时间复杂度是难以接受的.Can we do better? 先来看看解法2存在的缺陷. 1) 由于回文串长度的奇偶性造成了不同性质的对称轴位置,解法2要对两种情况 ...
- 51nod1089(最长回文子串之manacher算法)
题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 题意:中文题诶~ 思路: 我前面做的那道回文子串的题 ...
- 求最长回文子串:Manacher算法
主要学习自:http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html 问题描述:回文字符串就是左右 ...
- 最长回文子串(Manacher算法)
回文字符串,想必大家不会不熟悉吧? 回文串会求的吧?暴力一遍O(n^2)很简单,但当字符长度很长时便会TLE,简单,hash+二分搞定,其复杂度约为O(nlogn), 而Manacher算法能够在线性 ...
- 计算字符串的最长回文子串 :Manacher算法介绍
转自: http://www.open-open.com/lib/view/open1419150233417.html Manacher算法 在介绍算法之前,首先介绍一下什么是回文串,所谓回文串,简 ...
- hihocoder #1032 : 最长回文子串【 manacher算法实现 】
#1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...
- 51Nod 1089 最长回文子串 V2 —— Manacher算法
题目链接:https://vjudge.net/problem/51Nod-1089 1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: ...
- 51 Nod 1089 最长回文子串(Manacher算法)
1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 回文串是指aba.abba.cccbccc.aaa ...
随机推荐
- struts2之actionSupport学习
actionSupport在手工完成字段验证,显示错误消息,国际化等情况下推荐使用.
- Linux 学习(二)
Linux相关命令 命令 说明 startx 当前用户界面切换至图形界面 init5 切换至另一用户的图形化界面 init3 从图形界面切换回文本界面 pwd 显示当前用户路径 logout 注销 s ...
- python学习笔记(4)——list[ ]
发现个问题,python与C.JAVA等语言相比学习障碍最大差别居然在于版本更迭!这是python官方造的虐啊... 有时针对某问题去google答案,或者自己去博客找共性解答,会出现相互矛盾或者与你 ...
- (转)Hibernate框架基础——映射集合属性
http://blog.csdn.net/yerenyuan_pku/article/details/52745486 集合映射 集合属性大致有两种: 单纯的集合属性,如像List.Set或数组等集合 ...
- HDU_1506_Largest Rectangle in a Histogram_dp
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Jqueryd的一些 总结
JSP层 /*发送data 主要有三种方式:1.json 数组(推荐1)2.url拼接3.表单的序列化 serialize*/ <script type="text/javascrip ...
- 省市区json结构
[ { "label": "北京市", "value": "北京市", "children": [ ...
- Django 命令行调用模版渲染
首先我们需要进入 Django 的 shell python manage.py shell 渲染模版中的 name from django.template import Context,Templ ...
- 【03】json使用
[03]json使用 把 JSON 文本转换为 JavaScript 对象 JSON 最常见的用法之一,是从 web 服务器上读取 JSON 数据(作为文件或作为 HttpRequest),将 J ...
- JavaSE 学习笔记之反射技术(二十四)
反射技术:其实就是动态加载一个指定的类,并获取该类中的所有的内容.而且将字节码文件封装成对象,并将字节码文件中的内容都封装成对象,这样便于操作这些成员.简单说:反射技术可以对一个类进行解剖. 反射的好 ...