回文串---Hotaru's problem
Description
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
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 , descripting a sequence.
Output
We guarantee that the sum of all answers is less than 800000.
Sample Input
10
2 3 4 4 3 2 2 3 4 4
Sample Output
Source
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=;
int p[*N];
int s[*N],str[*N];
int n; void kp()
{
int mx=;
int id;
for(int i=;i<n;i++)
{
if(mx>i)
p[i]=min(p[*id-i],p[id]+id-i);
else
p[i]=;
for( ;i+p[i]<n;)
{
if(str[i+p[i]]==str[i-p[i]])
{
if(str[i+p[i]]==) p[i]++;
else
{
p[i]+=;
}
}
else break;
}
if(p[i]+i>mx)
{
mx=p[i]+i;
id=i;
}
}
} void init()
{
str[]=;
str[]=;
for(int i=; i<n; i++)
{
str[i*+]=s[i];
str[i*+]=;
}
n=n*+;
} int main()
{
int T,Case=;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&s[i]);
if(n<)
{
printf("%d",);
}
init();
kp();
///cout<<p[11]<<"++"<<p[15]<<endl;
int sum=0;
for(int i=;i<=n-;i=i+)
{
if(p[i]-<=sum/*) continue;
int t=p[i];
for(int j=t-;j>=;j=j-)
{
if(j<=sum/*) break;
if(i+j>n) continue;///可能这儿超时;
if(p[i+j]->=j)
{
sum=max(sum,j/*);
break;
}
}
}
printf("Case #%d: %d\n",Case++,sum);
}
return ;
}
回文串---Hotaru's problem的更多相关文章
- HDU 5371(2015多校7)-Hotaru's problem(Manacher算法求回文串)
题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- POJ 1159 回文串-LCS
题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文 ...
- BZOJ 2342 回文串-Manacher
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2342 思路:先跑一遍Manacher求出p[i]为每个位置为中心的回文半径,因为双倍回文串 ...
- BZOJ 2565 回文串-Manacher
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2565 题意:中文题 思路:定义L[i],R[i].表示以i为左端点/右端点时,最长回文串长 ...
- POJ 3974 回文串-Manacher
题目链接:http://poj.org/problem?id=3974 题意:求出给定字符串的最长回文串长度. 思路:裸的Manacher模板题. #include<iostream> # ...
- 【BZOJ】3676: [Apio2014]回文串
http://www.lydsy.com/JudgeOnline/problem.php?id=3676 题意:给一个串求回文串×出现次数的最大值.(|S|<=300000) #include ...
- 回文串---Best Reward
HDU 3613 Description After an uphill battle, General Li won a great victory. Now the head of state ...
- 回文串--- Girls' research
HDU 3294 Problem Description One day, sailormoon girls are so delighted that they intend to resear ...
随机推荐
- Swift之手势总结
手势个人笔记,代码简单,不过多赘述. 建立手势只需三步 建立和设置手势识别器实例,设置target,action,指定一些属性 附加识别器到视图 实现action方法 1.首先还是创建一个view @ ...
- C#集合--ICollection接口和IList接口
虽然列举接口提供了一个协议,用于向前的方式遍历集合,但它们没有提供一种机制来确定集合的大小,通过索引访问集合的成员,搜索集合,或修改集合.为了实现这些功能,.NET Framework定义了IColl ...
- PHP 常用获取路径代码
<? //获取当前的域名: echo $_SERVER['SERVER_NAME']; //获取来源网址,即点击来到本页的上页网址 echo $_SERVER["HTTP_REFERE ...
- uniGUI试用笔记(十四)TUniTreeView的CheckBox
TUniTreeView目前版本没有封装CheckBox功能,所以需要手工处理,幸好0.99版提供部分代码了,修改过程如下: 1.uniGUIAbstractClasses.pas单元中修改基类TUn ...
- 3种归并操作js代码
/**良哥的*/ function merge(a, b) { var aLen = a.length, bLen = b.length, maxLen = Math.max(aLen, bLen), ...
- Java数组,去掉重复值、增加、删除数组元素
import java.util.List; import java.util.ArrayList; import java.util.Set; import java.util.HashSet; p ...
- myeclipse不编译解决方法
在开发中经常遇到myeclipse不编译的情况,但不同情况的解决方法又不一样,今天同样是遇到此类情况,在网上狂搜,终于找到一篇好文,它囊括了解决这种情况的常用的方法,现在发出来与大家分享.我遇到的情况 ...
- jsp页面间传递参数 中文乱码问题(zz)
jsp页面间传递参数 中文乱码问题 1.传递参数 var url = "*****Test.jsp?param1="+encodeURI(encodeURI(str));//对 ...
- Qt 判断文件是否存在
包含头文件:<QFileInfo> 代码: QFileInfo file("文件路径"); if(file.exists()==false){文件不存在:}
- [C++] memset 和sizeof 的使用注意
因为使用C++写小题目时经常需要清除数组,这里记录下Memset函数的sizeof运算符的使用注意. memset的特点是:将给定地址后连续的内存(包括给定地址),逐个byte初始化为参数中指明的值. ...