题意:给你一个序列,求最长的两段回文子串,要求他们共用中间的一半。

思路:利用Manacher求出p[i]表示的当前位置的最长回文串长度,然后把每一个长度大于等于2的回文串的左区间和右区间分别放到两个数组里面,由于做manacher时添加了特殊的数字,所以处理的时候稍微注意一下。

  然后把左右区间按照左端点排序,接着根据尺取法来找答案中的那一段重合的部分。对于每一段右区间R[i]而言,只有L[j].right<=R[i].right && L[j].left<=R[i].right 就是有效的,对于这样的情况所得到的有效值就是L[j].right-R[i].left+1, 当然,当L[j].left>R[i].right时,就不能够再处理R[i]了,而是去处理R[i+1]。

 #include <iostream>
#include <cstdio>
#include <algorithm>
#define LL long long
#define MAXN 100005
using namespace std;
int n;
LL s[MAXN * ];
int p[MAXN];
int f[MAXN];
int m;
struct Node{
int left, right;
int mm;
Node(int left = , int right = ):left(left), right(right){
mm = right - left + ;
};
bool operator < (const Node & b) const{
return left < b.left;
}
};
vector<Node> L, R;
void manacher(){
int res = , id = ;
for(int i = ; i <= m; i++) {
if(res > i){
p[i] = min(p[ * id - i], res - i);
}
else{
p[i] = ;
}
//p[i] = mx > i? min(mp[2*id-i], mx-i): 1;
while(s[i + p[i]] == s[i - p[i]]){
p[i]++;
}
//while(s[i+mp[i]] == s[i-mp[i]]) mp[i]++;
if(i + p[i] > res) {
res = i + p[i];
id = i;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // OPEN_FILE
int T;
scanf("%d", &T);
int cas = ;
while(T--){
scanf("%d", &n);
// char ch;
m = ;
s[m++] = -;
s[m++] = -;
//LL x;
for(int i = ; i < n; i++){
scanf("%I64d", &s[m++]);
s[m++] = -;
}
s[m++] = -;
m--;
manacher();
L.clear();
R.clear();
for(int i = ; i <= m; i += ){
if(s[i] != - || p[i] == ) continue;
int x = (i / ) - ;
int y = x - ((p[i] - ) / ) + ;
L.push_back(Node(y, x));
x++;
y = x + ((p[i] - ) / ) - ;
R.push_back(Node(x, y));
//printf("%d ", p[i] -1);
}
sort(L.begin(), L.end());
sort(R.begin(), R.end());
/*for(int i = 0; i < R.size(); i++){
printf("%d %d\n", R[i].left, R[i].right);
}*/
int t = ;
int ans = ;
for(int i = ; i < R.size(); i++){
while(t < L.size() && L[t].left <= R[i].left){
if(R[i].left <= L[t].right && L[t].right <= R[i].right){
ans = max(ans, L[t].right - R[i].left + );
}
t++;
}
}
printf("Case #%d: %d\n", cas++, ans * );
}
}

HDU 5371 Hotaru's problem Manacher+尺取法的更多相关文章

  1. Hdu 5371 Hotaru's problem (manacher+枚举)

    题目链接: Hdu 5371 Hotaru's problem 题目描述: 给出一个字符串N,要求找出一条N的最长连续子串.这个子串要满足:1:可以平均分成三段,2:第一段和第三段相等,3:第一段和第 ...

  2. HDU 5371——Hotaru's problem——————【manacher处理回文】

    Hotaru's problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. 2015 Multi-University Training Contest 7 hdu 5371 Hotaru's problem

    Hotaru's problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  4. HDU 5371 Hotaru's problem (Manacher,回文串)

    题意:给一个序列,找出1个连续子序列,将其平分成前,中,后等长的3段子序列,要求[前]和[中]是回文,[中]和[后]是回文.求3段最长为多少?由于平分的关系,所以答案应该是3的倍数. 思路:先Mana ...

  5. Manacher HDOJ 5371 Hotaru's problem

    题目传送门 /* 题意:求形如(2 3 4) (4 3 2) (2 3 4)的最长长度,即两个重叠一半的回文串 Manacher:比赛看到这题还以为套个模板就行了,因为BC上有道类似的题,自己又学过M ...

  6. POJ 3320 Jessica's Reading Problem (尺取法)

    Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...

  7. POJ:3320-Jessica's Reading Problem(尺取法)

    Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15224 Accepted: ...

  8. 题解报告:poj 3320 Jessica's Reading Problem(尺取法)

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  9. HDU - 6103 :Kirinriki(不错的尺取法)

    We define the distance of two strings A and B with same length n is dis A,B =∑ i=0 n−1 |A i −B n−1−i ...

随机推荐

  1. java zyUpload 实现多文件上传

    1.html部分 <form enctype="multipart/form-data"> <label>请选择文件</label> <i ...

  2. axios简单封装

    写在最前面 新手前端刚刚接触vue,感觉真的好用.项目中需要使用axios,然后学习了一下.借鉴网上一些大佬的经验,现在分享一下axios的简单封装,如果有什么错误的地方,请大家指出. axios安装 ...

  3. centos6.5安装配置nginx+php+mysql+httpsqs+ttserver+redis

    一.准备 1.nginx, http://nginx.org/download/nginx-1.5.12.tar.gz 2.pcre, ftp://ftp.csx.cam.ac.uk/pub/soft ...

  4. ORACLE数据库字符集处理

    简介: ORACLE数据库字符集,即Oracle全球化支持(Globalization Support),或即国家语言支持(NLS)其作用是用本国语言和格式来存储.处理和检索数据.利用全球化支持,OR ...

  5. CCEditBox/CCEditBoxImplAndroid

    #ifndef __CCEDITBOXIMPLANDROID_H__ #define __CCEDITBOXIMPLANDROID_H__ #include "cocos2d.h" ...

  6. 2、java注释、标识符、数据类型、类型转换

    一.三种注释:单行注释.多行注释.文档注释(只能在类前或者方法前,@author作者) 二.java使用的编码为unicode码[0-65535]   包含ASCII码,在0-255中 ASCII码( ...

  7. Hadoop - YARN NodeManager 剖析

    一 概述         NodeManager是执行在单个节点上的代理,它管理Hadoop集群中单个计算节点,功能包含与ResourceManager保持通信,管理Container的生命周期.监控 ...

  8. OCP-1Z0-051-题目解析-第50题

    50. SLS is a private synonym for the SH.SALES table.  The user SH issues the following command:  DRO ...

  9. c++命名规范与代码风格

    http://blog.sina.com.cn/s/blog_a3a8d0b1010100uw.html http://www.cnblogs.com/len3d/archive/2008/02/01 ...

  10. Android 中如何使用 enum / 枚举

    如何在Android开发中合理的使用enum 欢迎大家访问我的Github开源库,这里有好玩的App源码,想和大家分享.https://github.com/ChoicesWang 转载请注明:htt ...