HDU 5371 Hotaru's problem (Manacher,回文串)
题意:给一个序列,找出1个连续子序列,将其平分成前,中,后等长的3段子序列,要求【前】和【中】是回文,【中】和【后】是回文。求3段最长为多少?由于平分的关系,所以答案应该是3的倍数。
思路:先Manacher求最长子串,利用期间所记录的P 数组,穷举一下所有可能的前两串,再用O(1)时间判断第3串是否符合要求。
具体做法:
(1)P[i]记录的是以i为中心,从i-P[i]+1到i+P[i]-1这段都是回文。由于前两段之和必为偶数,所以必须选取str[i]为'#'的。
(2)扫一遍每个'#',以其最长的回文开始穷举(仅需将P[i]--即可,然后找到右边对应的'#',判断P[i]是不是大于所穷举的长度),直到3段都满足要求了,跳出此‘#’,换下一个。
#include <iostream>
#include <cstring>
#include <vector>
#include <stdio.h>
using namespace std;
const int N=;
int n;
int s[N*];
int P[N*]; int cal(int len)
{
if(n<) return ;
memset(P,,sizeof(P));
int id=, mx=;
P[]=;
P[]=;
for(int i=; i<len; i++)
{
P[i] =i>mx? : min( P[*id-i], mx-i);
while(s[i+P[i]]==s[i-P[i]]) P[i]++;
if(i+P[i]>mx)
{
id=i;
mx=i+P[i];
}
}
int ans=;
for(int i=; i+<len; i+=)
{
int tag=P[i]-;
if( tag> && tag>ans )
{
while( P[i+tag]<=tag && tag>ans ) tag--;
if(tag>ans) ans=tag;
}
} return ans/*;
} int main()
{
//freopen("input.txt", "r", stdin);
int t, tmp, j=;
cin>>t; while(t--)
{
scanf("%d", &n);
s[]=-;
s[]=-;
for(int i=,j=; i<n; i++)
{
scanf("%d",&tmp);
s[j++]=tmp;
s[j++]=-;
}
s[n*+]=-; printf("Case #%d: %d\n", ++j, cal( n*+ ));
}
return ;
}
AC代码
HDU 5371 Hotaru's problem (Manacher,回文串)的更多相关文章
- Hdu 5371 Hotaru's problem (manacher+枚举)
题目链接: Hdu 5371 Hotaru's problem 题目描述: 给出一个字符串N,要求找出一条N的最长连续子串.这个子串要满足:1:可以平均分成三段,2:第一段和第三段相等,3:第一段和第 ...
- HDU 5371 Hotaru's problem Manacher+尺取法
题意:给你一个序列,求最长的两段回文子串,要求他们共用中间的一半. 思路:利用Manacher求出p[i]表示的当前位置的最长回文串长度,然后把每一个长度大于等于2的回文串的左区间和右区间分别放到两个 ...
- [hdu5371 Hotaru's problem]最大回文半径
题意:在一个字符串里面找最长的[A][B][A]子串,其中[A][B]是回文串,[A]和[B]的长度相等 思路:[A][B]是回文串,所以[B][A]也是回文串.先预处理出每个点的最大回文半径Ri,枚 ...
- HDU3068 最长回文 MANACHER+回文串
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...
- Manacher回文串算法学习记录
FROM: http://hi.baidu.com/chenwenwen0210/item/482c84396476f0e02f8ec230 #include<stdio.h> #inc ...
- Best Reward---hdu3613(manacher 回文串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意就是给你一个串s 然后求把s分成两部分之后的价值总和是多少,分开的串 如果是回文那么价值就是 ...
- 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】
O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...
- UVA 12378 Ball Blasting Game 【Manacher回文串】
Ball Blasting Game Morteza is playing a ball blasting game. In this game there is a chain of differe ...
- HDU 5371——Hotaru's problem——————【manacher处理回文】
Hotaru's problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- Subclasses
Given a collection of numbers, return all possible subclasses. public class Solution { public List&l ...
- Javascript 正则表达式_3
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- ActionResult 返回类型
类名 抽象类 父类 功能 ContentResult 根据内容的类型和编码,数据内容. EmptyResult 空方法. FileResult abstract 写入文件内容,具体 ...
- (7)nehe教程1 创建一个OpenGL窗口:
不要用那个nehe ndk了 误人子弟! 转自: 一个窗口,代码可真多啊 http://www.yakergong.net/nehe/ 在这个教程里,我将教你在Windows环境中创建OpenGL程序 ...
- sql主键的一点重要理解
sql只会读取数据,不会自动设置主键,所以绑定数据后要设置主键(前台) 不管是int或者uniqueidentifier只要类型对得上就可以用,int自增其实没什么太大优势(但是通常都会用自增来做,从 ...
- C# 任意类型数据转JSON格式
/// <summary> /// List转成json /// </summary> /// <typeparam name="T">< ...
- 项目中遇到的 linq datatable select
1如何使用DataTable.Select选出来的Rows生成新的DataTable?DataTable dt = 数据源;DataTable dtt = new DataTable();dtt=dt ...
- spring 注入失败
最近发现autowired注入总是失败. 总结下: 一个bean 要么都通过getter setter加上配置文件配置注入. <bean id="temResetService&quo ...
- C#格式化输出
double a = 12354.365; Console.WriteLine(string.Format("{0:f4}", a)); 输出a的四位小数
- 如何计算一个字符串表示的计算式的值?——C_递归算法实现
在<C程序设计伴侣>的8.7.3 向main()函数传递数据这一小节中,我们介绍了如何通过main()函数的参数,向程序传递两个数据并计算其和值的简单加法计算器add.exe.这个程序,好 ...