HDU 5371(2015多校7)-Hotaru's problem(Manacher算法求回文串)
题目地址:HDU 5371
题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列。该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称。假设存在求最长的符合这样的条件的序列。
思路:用Manacher算法来处理回文串的长度,记录下以每个-1(Manacher算法的插入)为中心的最大回文串的长度。
然后从最大的開始穷举,仅仅要p[i]-1即能得出以数字为中心的最大回文串的长度,然后找到右边相应的’-1’。推断p[i]是不是大于所穷举的长度,假设当前的满足三段,那么就跳出,继续下一个以数字为中心的回文串。
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef __int64 LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
const double esp=1e-7;
const int Maxn=100005;
int str[Maxn];
int s[Maxn*2];
int p[Maxn*2];
int n;
void Manacher()
{
memset(p,0,sizeof(p));
s[0]=-2;
s[1]=-1;
for (int i=0;i<n;i++) {
s[i*2+2]=str[i];
s[i*2+3]=-1;
}
int MaxL=0,id=0;
for (int i=2; i<2*n+1; i++) {
if (p[id]+id>i)
p[i]=min(p[2*id-i],p[id]+id-i);
else
p[i]=1;
while (s[i-p[i]]==s[i+p[i]])
p[i]++;
if(id+p[id]<i+p[i])
id = i;
if (p[i]>MaxL)
MaxL=p[i];
}
}
int main()
{
int T,i;
int Max;
int ans;
int icase=1;
scanf("%d",&T);
while(T--) {
Max=0;
scanf("%d",&n);
for(i=0; i<n; i++)
scanf("%d",&str[i]);
Manacher();
for(i=3; i<2*n+1;i+=2){
ans=p[i]-1;
while(ans>Max&&p[i+ans]<ans)
ans--;
Max=max(Max,ans);
}
printf("Case #%d: %d\n",icase++,Max/2*3);
}
return 0;
}
HDU 5371(2015多校7)-Hotaru's problem(Manacher算法求回文串)的更多相关文章
- HDU 5371 (2015多校联合训练赛第七场1003)Hotaru's problem(manacher+二分/枚举)
pid=5371">HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分同样,第一部分与第二部分对称. 如今给你一个长为n(n<10^5)的序 ...
- Hdu 3294 Girls' research (manacher 最长回文串)
题目链接: Hdu 3294 Girls' research 题目描述: 给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.... ...
- hdu 3294 manacher 求回文串
感谢: http://blog.csdn.net/ggggiqnypgjg/article/details/6645824/ O(n)求给定字符串的以每个位置为中心的回文串长度. 中心思想:每次计算位 ...
- hdu5371Hotaru's problem manacher算法
//给一个序列.让求其最大子序列 //这个序列由三段组成.第一段和第二段对称,第一段和第三段一样 //manacher算法求得p[i] //枚举第二段的起点和长度,得到结果 #include<c ...
- HDU 3613 Best Reward ( 拓展KMP求回文串 || Manacher )
题意 : 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串,那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问 ...
- HDU 3068 最长回文 (Manacher最长回文串)
Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等 Input 输 ...
- hdu 5371 Hotaru's problem【manacher】
题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=5371 题意: 给出一个长度为n的串,要求找出一条最长连续子串.这个子串要满足:1:能够平均分成三段 ...
- Hotaru's problem(hdu5371+Manacher)多校7
Hotaru's problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- hdu 5288||2015多校联合第一场1001题
pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...
随机推荐
- python清除字符串中无用字符
将列表val_list中包含的非法字符去掉,illegal_char是非法字符列表 def clear(): illegal_char = [' ','#','%','_','@'] tmp_list ...
- 关键字搜索高亮jQuery插件
// textSearch.js v1.0 文字,关键字的页面纯客户端搜索// 2010-06-23 修复多字母检索标签破碎的问题// 2010-06-29 修复页面注释显示的问题// 2013-05 ...
- 「 Luogu P2196 」 挖地雷
# 解题思路 跑 $\text{n}$ 遍 $\text{spfa}$ 并记录路径,找到比当前最长路长的就更新答案,并且将路径也更新,注意起点的处理. # 附上代码 #include <iost ...
- BZOJ 3876 支线剧情 有源汇有上下界最小费用可行流
题意: 给定一张拓扑图,每条边有边权,每次只能从第一个点出发沿着拓扑图走一条路径,求遍历所有边所需要的最小边权和 分析: 这道题乍一看,可能会想到什么最小链覆盖之类的,但是仔细一想,会发现不行,一是因 ...
- 基于Redis的三种分布式爬虫策略
前言: 爬虫是偏IO型的任务,分布式爬虫的实现难度比分布式计算和分布式存储简单得多. 个人以为分布式爬虫需要考虑的点主要有以下几个: 爬虫任务的统一调度 爬虫任务的统一去重 存储问题 速度问题 足够“ ...
- ubuntu 安装python 编程环境
1. 安装python sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get updatesudo apt-get install pyt ...
- MySQL-----改
改 **修改用户名** rename user 'username'@'IP address' to 'new username'@'IP address'; **修改密码** set passwor ...
- 1002. A+B for Polynomials (25) (浮点数判0)
This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file con ...
- TYVJ1716 上帝造题的七分钟
时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 裸体就意味着身体. 描述 “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵.第二分钟,L说, ...
- 【IntelliJ】IntelliJ IDEA常用设置及快捷键以及自定义Live templates
IntelliJ IDEA是一款非常优秀的JAVA编辑器,初学都可会对其中的一些做法感到很别扭,刚开始用的时候我也感到很不习惯,在参考了网上一些文章后在这里把我的一些经验写出来,希望初学者能快速适应它 ...