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

思路:利用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. windows下matlab代码到ubuntu下中文注释出现乱码

    转自:https://blog.csdn.net/kouyi5627/article/details/81513329 环境:Ubuntu18.04,Matlab R2017b. 把matlab文件从 ...

  2. sed 替换 引用变量值,记录一个自己学习错误的地方。

    先上脚本,脚本的目的是虚拟机克隆-连接克隆,然后修改ip这个搞定,修改hostname就很简单了 declare oldipdeclare -i Anamedeclare newipoldip=`ca ...

  3. CodeForces - 552E Vanya and Brackets

    Vanya and Brackets Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u ...

  4. 多线程相互排斥--mutex

    多线程之线程同步Mutex (功能与Critial Sections同样,可是属于内核对象,訪问速度较慢.能够被不同进程调用) 一 Mutex     相互排斥对象(mutex)内核对象可以确保线程拥 ...

  5. 面试题:Student s = new Student();在内存中做了哪些事情?即创建一个对象做了哪些事情

    lStudent s = new Student();在内存中做了哪些事情? •载入Student.class文件进内存(方法区) •在栈内存为s开辟空间 •在堆内存为学生对象开辟空间 •对学生对象的 ...

  6. mysql-管理事务

    一.介绍 mysql支持几种基本的数据库引擎,其中MYSQL的两种最基本的引擎MyISAM和InnoDB,其中只有InnoDB支持事务管理. 事务处理:可以用来维护数据库的完整性,他保证成批的MySQ ...

  7. Invalid property 'sentinels' of bean class redis spring 错误修改

    /* * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Vers ...

  8. Android微信智能心跳方案 Android微信智能心跳方案

    原文地址: http://mp.weixin.qq.com/s?__biz=MzAwNDY1ODY2OQ==&mid=207243549&idx=1&sn=4ebe4beb81 ...

  9. 基于UDP的DDos反射放大攻击

    转自:https://www.us-cert.gov/ncas/alerts/TA14-017A Protocol Bandwidth Amplification Factor DNS 28 to 5 ...

  10. DefaultView 的作用(对DataSet查询出的来数据进行排序)

    DefaultView 的作用 收藏    一直以来在对数据进行排序, 条件查询都是直接重复构建SQL来进行, 在查询次数和数据量不多的情况下倒没觉得什么, 但慢慢得, 当程序需要对大量数据椐不同条件 ...