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

思路:利用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. C++ vector基本用法

    转自金河http://www.cnblogs.com/wang7/archive/2012/04/27/2474138.html 1 基本操作 (1)头文件#include<vector> ...

  2. pytorch 2 variable 变量

    import torch from torch.autograd import Variable tensor = torch.FloatTensor([[1, 2], [3, 4]]) variab ...

  3. 紫书 例题11-10 UVa 1349 (二分图最小权完美匹配)

    二分图网络流做法 (1)最大基数匹配.源点到每一个X节点连一条容量为1的弧, 每一个Y节点连一条容量为1的弧, 然后每条有向 边连一条弧, 容量为1, 然后跑一遍最大流即可, 最大流即是最大匹配对数 ...

  4. [MST] Build Forms with React to Edit mobx-state-tree Models

    We will expand our UI, and give the user the possibility to edit his wishlist. We will use the earli ...

  5. java 经常使用測试框架

    1. 经常使用单元化測试框架 junit4 , TestNG 能够通过注解 @Before @After @BeforeClass @AfterClass 分别作方法与类级的初始化与结束动作. tes ...

  6. elasticsearch中的几个概念总结

    1.Geo spatial search : 地理空间搜索,可以在搜索查询中指定的某一距离内查找所要的内容.也可以返回以当前为圆心,逐渐添加圆的半径.直到找到所匹配到的内容. 參考:http://ww ...

  7. 如何将网站升级为HTTPS协议(整理)

    如何将网站升级为HTTPS协议(整理) 一.总结 一句话总结: 获取证书(有免费有付费):证书是一个二进制文件,里面包含经过认证的网站公钥和一些元数据,要从经销商购买. 安装证书:证书可以放在/etc ...

  8. 语法错误: unexpected ''); ?></span></span></h2> ' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'

    语法错误: unexpected ''); ?></span></span></h2>' (T_CONSTANT_ENCAPSED_STRING), expe ...

  9. 36.创建自定义的指令directive

    转自:https://www.cnblogs.com/best/tag/Angular/ 1. <html> <head> <meta charset="utf ...

  10. 131.typename在嵌套类中的作用

    #include <iostream> using namespace std; class myit { public: static int num; class itit { }; ...