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 ...
随机推荐
- Android图像处理之BitMap(2)
Bitmap 相关 1. Bitmap比较特别 因为其不可创建 而只能借助于BitmapFactory 而根据图像来源又可分以下几种情况: * png图片 如:R.drawable.tianjin J ...
- 在你的Android手机上运行Linux
之前试过许多方法(也就几种),像什么Complete Linux Installer,Debian noroot,利用已有的Linux构造Bootstrap之类,要么就是复杂得要命(调了两天没有调出来 ...
- Xshell连接Centos7.5和yum
目 录 第1章 Centos7 IP地址的配置 1 1.1 第一种配置ip方法(nmtui) 1 1.2 第二种 修改网卡配置文件 5 1.2.1 使用cat查看配置文件 5 ...
- MySQL数据库初识
认识数据库 1 什么是数据(Data) 描述事物的符号记录称为数据,描述事物的符号既可以是数字,也可以是文字.图片,图像.声音.语言等,数据由多种表现形式,它们都可以经过数字化后存入计算机 在计算机中 ...
- 04003_CSS
1.DIV相关的技术 (1)DIV是一个html标签,一个块级元素(单独显示一行).它单独使用没有任何意义,必须结合CSS来使用,主要用于页面的布局: (2)Span是一个html标签,一个内联元素( ...
- heap corruption detected VS2015 C语言 报错
申请动态内存时,申请的单元数为n,可用下标为0~n-1 但实际使用时超过了该范围,就会报这个错
- SPOJ GSS6 Can you answer these queries VI
Can you answer these queries VI Time Limit: 2000ms Memory Limit: 262144KB This problem will be judge ...
- POJ-1679 The Unique MST,次小生成树模板题
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Description Given a connected undirec ...
- PatentTips – EMC Virtual File System
BACKGROUND OF THE INVENTION 1. Field of the Invention The present invention generally relates to net ...
- msp430入门学习12
msp430的定时器--Timer_A(定时器A) msp430入门学习