HDU 3068 Manacher裸题

 #include <cstdio>
#include <cstring>
const int Maxn=;
char Str[Maxn<<],STR[Maxn<<];
int Len,x,P[Maxn<<],Id,Mx;
inline int Max(int x,int y) {return x>y?x:y;}
inline int Min(int x,int y) {return x>y?y:x;}
inline Init()
{
Len=strlen(Str+);
for (int i=;i<=Len;i++) STR[i]=Str[i];
Str[]='$'; Str[]='#';
for (int i=;i<=Len;i++) Str[i<<]=STR[i],Str[i<<|]='#';
Len=Len<<|; Id=;Mx=;
}
inline int Manacher()
{
for (int i=;i<=Len;i++)
{
if (Mx>i) P[i]=Min(P[*Id-i],Mx-i); else P[i]=;
while (Str[i+P[i]]==Str[i-P[i]]) P[i]++;
if (i+P[i]>Mx) Mx=P[i]+i,Id=i;
}
int Ret=;
for (int i=;i<=Len;i++) Ret=Max(Ret,P[i]);
return Ret-;
}
int main()
{
while (scanf("%s",Str+)!=EOF)
{
Init();
printf("%d\n",Manacher());
}
return ;
}

C++

BZOJ 3790 线段求交

用Manacher把最大的回文半径求出然后线段求交即可

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#define mp make_pair
#define pb push_back
#define PA pair<int,int>
#define fi first
#define se second
using namespace std;
const int Maxn=;
const int Inf=0x3f3f3f3f;
char S[Maxn],Str[Maxn];
int P[Maxn],c[Maxn];
int Len;
vector<PA> Line;
inline int Min(int x,int y) {return x>y?y:x;}
inline int lowbit(int x) {return x&-x;}
inline int Query(int x)
{
if (x==) return ;
int Ret=Inf;
for (int i=x;i<=Len;i+=lowbit(i)) Ret=Min(Ret,c[i]);
return Ret;
}
inline void Update(int x,int y)
{
for (int i=x;i;i-=lowbit(i)) c[i]=Min(c[i],y);
}
inline void Manacher()
{
int m=Len<<|;
for (int i=;i<=Len;i++)
{
S[i<<]=Str[i];
S[i<<|]='#';
}
S[]='#'; S[]='&'; S[m+]='^';
int Mx=,Id=;
for (int i=;i<=m;i++)
{
if (i<Mx) P[i]=Min(Mx-i,P[*Id-i]); else P[i]=;
while (S[i-P[i]]==S[i+P[i]]) P[i]++;
if (i+P[i]>Mx) Mx=i+P[i],Id=i;
int l=(i-P[i])/+,r=(i+P[i])/-;
if (l<=r) Line.pb(mp(r,l));
}
}
int main()
{
while (scanf("%s",Str+)!=EOF)
{
Len=strlen(Str+); Line.clear();
Manacher();
sort(Line.begin(),Line.end());
for (int i=;i<=Len;i++) c[i]=Inf;
int Ans=Inf;
for (int i=;i<Line.size();i++)
{
int x=Query(Line[i].se-)+;
Update(Line[i].fi,x);
if (Line[i].fi==Len) Ans=Min(Ans,x);
}
printf("%d\n",Ans-);
}
return ;
}

C++

Manacher的更多相关文章

  1. HDU3068 回文串 Manacher算法

    好久没有刷题了,虽然参加过ACM,但是始终没有融会贯通,没有学个彻底.我干啥都是半吊子,一瓶子不满半瓶子晃荡. 就连简单的Manacher算法我也没有刷过,常常为岁月蹉跎而感到后悔. 问题描述 给定一 ...

  2. manacher算法专题

    一.模板 算法解析:http://www.felix021.com/blog/read.php?2040 *主要用来解决一个字符串中最长回文串的长度,在O(n)时间内,线性复杂度下,求出以每个字符串为 ...

  3. BZOJ2342 Manacher + set

    题一:别人介绍的一道题,题意是给出一个序列,我们要求出一段最常的连续子序列,满足:该子序列能够被平分为三段,第一段和第二段形成回文串,第二段和第三段形成回文串. 题二:BZOJ2342和这题非常的相似 ...

  4. lintcode最长回文子串(Manacher算法)

    题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...

  5. Manacher's algorithm

    Manacher's algorithm 以\(O(n)\)的线性时间求一个字符串的最大回文子串. 1. 预处理 一个最棘手的问题是需要考虑最长回文子串的长度为奇数和偶数的情况.我们通过在任意两个字符 ...

  6. 1089 最长回文子串 V2(Manacher算法)

    1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 回文串是指aba.abba.cccbccc.aaaa ...

  7. Manacher's Algorithm 马拉车算法

    这个马拉车算法Manacher‘s Algorithm是用来查找一个字符串的最长回文子串的线性方法,由一个叫Manacher的人在1975年发明的,这个方法的最大贡献是在于将时间复杂度提升到了线性,这 ...

  8. 51nod1089(最长回文子串之manacher算法)

    题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 题意:中文题诶~ 思路: 我前面做的那道回文子串的题 ...

  9. HDU - 3948 后缀数组+Manacher

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3948 题意:给定一个字符串,求字符串本质不同的回文子串个数. 思路:主要参考该篇解题报告 先按照man ...

  10. LeetCode 5 Longest Palindromic Substring manacher算法,最长回文子序列,string.substr(start,len) 难度:2

    https://leetcode.com/problems/longest-palindromic-substring/ manacher算法相关:http://blog.csdn.net/ywhor ...

随机推荐

  1. Moving Tables(贪心或Dp POJ1083)

    Moving Tables Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28304   Accepted: 9446 De ...

  2. 405 Method Not Allowed

    今天在发布一个网站的时候遇到 标题上的问题,一直不明白是为何,刚开始以为是我的程序写的有问题,随即将项目发给同事来发布试试,在他的IIS上发布却没出现问题,一切正常,这可就怪了,于是想到了应该是IIS ...

  3. 20个Linux服务器安全强化建议(三)

    #11.配置iptables和TCPWrappers.   iptables 是一个Linux内核提供的,运行在用户空间的程序,它允许用户配置自己的防火墙策略.我们可以使用防火墙将不必要的流量过滤出去 ...

  4. 系统收到了多个不同的 Content-Disposition 标头。为了避免遭到 HTTP 响应拆分攻击,这种情况是不允许的。

    今天使用Struts2进行上传下载的时候发现了一个现象 我的Struts2配置文件 <action name="share_ExportExcel" class=" ...

  5. hdu2296Ring(ac自动机+dp)

    链接 dp[i][j]表示长度为i在节点J的时候的权值最大值,根据trie树转移一下就行,需要每次都取最小的,所以需要另开一数组保存字典序最小的状态. #include <iostream> ...

  6. Spring Boot Admin 的使用 2

    http://blog.csdn.net/kinginblue/article/details/52132113 ******************************************* ...

  7. update

    update `表名` set 字段名 =replace(字段名, '查找的内容','更改的内容') where 字段名 like '%查找的内容%'; update shangpin set cli ...

  8. [kylin] 部署kylin服务

    一.工具准备 zookeeper3.4.6 (hadoop.hbase 管理工具) Hadoop. Hbase1.1.4 Kylin1.5.0-HBase1.1.3 Jdk1.7.80 Hive 二. ...

  9. javascript的一些基础

    当复制的两个变量的地址不同时他们是不相等的如下代码所示 function getFunction(value){ return function(value){ return value; } } v ...

  10. ASP.NET 程序中删除文件夹导致session失效解决问题

    在Global中  Application_Start 添加 如: void Application_Start(object sender, EventArgs e)    {        //在 ...