Manacher's Algorithm 马拉车算法(求最长回文串)
作用:求一个字符串中的最长子串,同时还可以求所有子串的长度。
题目链接:
https://vjudge.net/contest/254692#problem/B
最长回文串长度的代码:
int Manacher(string s)
{
string t = "$#";
for (int i = ; i < s.size(); ++i)
{
t += s[i];
t += "#";
}
vector<int> p(t.size(), );
int maxx=;
int mx = , id = , resLen = , resCenter = ;
for (int i = ; i < t.size(); ++i)
{
// maxx=max(maxx,p[i]);
p[i] = mx > i ? min(p[ * id - i], mx - i) : ;
while (t[i + p[i]] == t[i - p[i]])
++p[i];
if (mx < i + p[i])
{
mx = i + p[i];
id = i;
}
if (resLen < p[i])
{
resLen = p[i];
resCenter = i;
}
maxx=max(maxx,p[i]);
}
return maxx-;
}
代码:
#include<bits/stdc++.h>
using namespace std;
# define maxn +
char str1[maxn];
char str2[maxn*];
int p[maxn*];
int l=;
int ma()
{
int id=,mx=;
int ans=;
str2[l]='\0';
for(int i=; i<l+; i++)p[i]=;
for(int i=; i<l; i++)
{
if(i<mx)p[i]=min(p[*id-i],mx-i);
else p[i]=;
while(str2[i-p[i]]==str2[i+p[i]])
p[i]++;
if(mx<i+p[i])
{
id=i;
mx=i+p[i];
}
ans+=p[i]/;
}
return ans;
}
int main()
{
int n;
scanf("%d",&n);
scanf("%s",str1);
str2[l++]='$';
str2[l++]='#';
for(int i=; i<n; i++)
{
if(str1[i]=='-')
{
if(l-!=)l-=;
}
else
{
str2[l++]=str1[i];
str2[l++]='#';
}
printf("%d",ma());
if(i!=n-)printf(" ");
}
printf("\n");
return ;
}
上面这个版本的manacher在处理多组输入的时候不如下面这个省时间。
void Manacher(int n)
{
int i, id=, len=; Estr[] = '$'; for(i=start[n]; i<start[n+]; i++)
{
Estr[len++] = '#';
Estr[len++] = MumStr[i]; suffix[i] = false;
prefix[i] = false;
}
Estr[len] = '#';
Estr[len+] = ; for(i=; i<len; i++)
{
p[i] = ; if(p[id]+id > i)
p[i] = min(p[id*-i], p[id]+id-i); while(Estr[ i+p[i] ] == Estr[ i-p[i] ])
p[i]++; if(p[id]+id < p[i]+i)
id = i;
}
}
Manacher's Algorithm 马拉车算法(求最长回文串)的更多相关文章
- Manacher's Algorithm 马拉车算法(最长回文串)
这个马拉车算法Manacher‘s Algorithm是用来查找一个字符串的最长回文子串的线性方法,由一个叫Manacher的人在1975年发明的,这个方法的最大贡献是在于将时间复杂度提升到了线性,这 ...
- Manacher算法 - 求最长回文串的利器
求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...
- hdu 3068 最长回文 (Manacher算法求最长回文串)
参考博客:Manacher算法--O(n)回文子串算法 - xuanflyer - 博客频道 - CSDN.NET 从队友那里听来的一个算法,O(N)求得每个中心延伸的回文长度.这个算法好像比较偏门, ...
- 小白月赛13 B小A的回文串 (马拉车算法求最长回文子串)
链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...
- leetcode 5 Longest Palindromic Substring(Manacher算法求最长回文串)
应用一下manacher算法就可以O(n)求出结果了.可以参考hdu3068 substr(start,length)函数是这样用的: substr 方法 返回一个从指定位置开始,并具有指定长度的子字 ...
- Manacher算法求最长回文串模板
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> ...
- Manacher算法(马拉车)求最长回文子串
Manacher算法求最长回文字串 算法思路 按照惯例((・◇・)?),这里只是对算法的一些大体思路做一个描述,因为找到了相当好理解的博客可以参考(算法细节见参考文章). 一般而言,我们的判断回文算法 ...
- manacher算法求最长回文子串
一:背景 给定一个字符串,求出其最长回文子串.例如: s="abcd",最长回文长度为 1: s="ababa",最长回文长度为 5: s="abcc ...
- manacher算法求最长回文子序列
一:背景 给定一个字符串,求出其最长回文子串.例如: s="abcd",最长回文长度为 1: s="ababa",最长回文长度为 5: s="abcc ...
随机推荐
- LeetCode 70. Climbing Stairs爬楼梯 (C++)
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- LeetCode 504. Base 7 (C++)
题目: Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "2 ...
- Four-Operations
开发环境:Eclipse 结对小伙伴:201306114416 陈键 (http://www.cnblogs.com/be-the-one/) 201306114452 吴舒婷 (http://www ...
- 第三次spring冲刺2
完成了对错题的收集,和做错题的功能,运用了android自带的SQLite数据库.
- ElasticSearch 2 (29) - 信息聚合系列之测试驱动
ElasticSearch 2 (29) - 信息聚合系列之测试驱动 摘要 我们可以用以下几页定义不同的聚合和它们的语法,但学习聚合的最佳途径就是用实例来说明.一旦我们获得了聚合的思想,以及如何合理地 ...
- 阿里云视频直播PHP-SDK接入教程
阿里云视频直播PHP-SDK接入教程 阿里云 视频直播 配置 及 PHP-SDK 接入教程 准备工作 域名管理 配置鉴权 地址生成器及DEMO演 ...
- 消息队列1:RabbitMQ解析并基于Springboot实战
RabbitMQ简介 AMQP:Advanced Message Queue,高级消息队列协议.它是应用层协议的一个开放标准,为面向消息的中间件设计,基于此协议的客户端与消息中间件可传递消息,并不受产 ...
- 用Setup Factory7.0怎样打包delphi的BDE?
BDE打包发布实例操作步骤如下: 使用软件:Setup Factory 7.0打包 把C:\Program Files\Common Files\Borland Shared中的所有文件和你的开发的应 ...
- 51Nod 1175 区间中第K大的数 (可持久化线段树+离散)
1175 区间中第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 一个长度为N的整数序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有 ...
- 自学huawei之路-6005-8AP设备启动界面
返回自学Huawei之路 自学huawei之路-AC6005-8AP设备启动界面 [YK-MES-MASTER] Please check whether system data has been c ...