http://wenku.baidu.com/link?url=WFI8QEEfzxng9jGCmWHoKn0JBuHNfhZ-tKTDMux34CeY8UNUwLVPeY5HA3TyoKU2XegXFPifjunarW-YmXFrP_m8-3DEhBu1MHxHghHqD0O

这篇讲的比较好,准备一个模板,做题的时候用。

void manacher()
{
int mx = , id = ;
for (int i = ; i <= n; i++)
{
if (mx > i)
p[i] = min(p[id * - i], mx - i);
else
p[i] = ;
while (a[i + p[i]] == a[i - p[i]])
p[i]++;
if (mx < p[i] + i)
{
mx = p[i] + i;
id = i;
}
}
}

这个初始化的时候是从1开始的

for (int i = ; i <= n; i++)
{
a[i * - ] = str[i];
a[i * ] = -;
}

其中p表示回文半径,str是原串,a表示添加字符之后的串

两个例题:

hdu5340

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm> using namespace std;
typedef long long LL;
const int maxn = ;
char str[maxn], a[maxn<<];
int p[maxn<<], L[maxn<<], R[maxn<<];
int n;
void manacher()
{
int mx = , id = ;
for (int i = ; i <= n; i++)
{
if (mx > i)
p[i] = min(p[id * - i], mx - i);
else
p[i] = ;
while (a[i + p[i]] == a[i - p[i]])
p[i]++;
if (mx < p[i] + i)
{
mx = p[i] + i;
id = i;
}
}
}
bool solve()
{
for (int i = ; i <= L[]; i++)
{
for (int j = ; j <= R[]; j++)
{
int l = L[i] + ;
int r = R[j] - ;
if (r - l <= )
continue;
int mid = (l + r) / ;
if (p[mid] >= r - mid + )
return true;
}
}
return false;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%s", str + );
n = strlen(str + );
for (int i = ; i <= n; i++)
{
a[i * - ] = str[i];
a[i * ] = '#';
}
n = n * - ;
a[] = '$';
a[n + ] = '@';
manacher();
memset(L, , sizeof(L));
memset(R, , sizeof(R));
for (int i = ; i <= n; i++)
{
int l = i - p[i] + ;
int r = i + p[i] - ;
if (l == )
L[++L[]] = r;
if (r == n)
R[++R[]] = l;
}
if (solve())
puts("Yes");
else
puts("No");
}
return ;
}

hdu5371

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = ;
int str[maxn], a[maxn<<];
int p[maxn<<];
int n;
void manacher()
{
int mx = , id = ;
for (int i = ; i <= n; i++)
{
if (mx > i)
p[i] = min(p[id * - i], mx - i);
else
p[i] = ;
while (a[i + p[i]] == a[i - p[i]])
p[i]++;
if (mx < p[i] + i)
{
mx = p[i] + i;
id = i;
}
}
}
void print()
{
for (int i = ; i <= n; i++)
printf("%d ", p[i]);
}
int main()
{
int T;
int kase = ;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
scanf("%d", &str[i]);
for (int i = ; i <= n; i++)
{
a[i * - ] = str[i];
a[i * ] = -;
}
int m = n;
n = n * - ;
a[] = -;
a[n + ] = -;
manacher();
int ans = ;
for (int i = ; i <= n; i += )
p[i] /= ;
for (int i = ; i <= n; i += )
{
if (p[i] * < ans)
continue;
for (int j = p[i]; j * >= ans; j--)
{
if (p[i + j * ] >= j)
{
ans = max(ans, j * );
break;
}
}
}
printf("Case #%d: %d\n", ++kase, ans);
}
return ;
}

Manacher算法求回文半径的更多相关文章

  1. HDU 5371(2015多校7)-Hotaru&#39;s problem(Manacher算法求回文串)

    题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...

  2. SPOJ STC02 - Antisymmetry(Manacher算法求回文串数)

    http://www.spoj.com/problems/STC02/en/ 题意:给出一个长度为n的字符串,问其中有多少个子串s可以使得s = s按位取反+翻转. 例如样例:11001011. 10 ...

  3. hdu5340—Three Palindromes—(Manacher算法)——回文子串

    Three Palindromes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. 马拉车算法——求回文子串个数zoj4110

    zoj的测评姬好能卡时间.. 求回文子串的个数:只要把p[i]/2就行了: 如果s_new[i]是‘#’,算的是没有中心的偶回文串 反之是奇回文串 /* 给定两个字符串s,t 结论:s,t不相同的第一 ...

  5. Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version)(Manacher算法+输出回文字符串)

    This is the hard version of the problem. The difference is the constraint on the sum of lengths of s ...

  6. Manacher算法求解回文字符串

    Manacher算法可以在\(O(N)\)时间内求解出一个字符串的所有回文子串(正反遍历相同的字串). 注:回文串显然有两种,一种是奇数长度,如abczcba,有一个中心字符z:另外一种是偶数个长度, ...

  7. 马拉车算法——求回文串起点hdu3294

    #include<bits/stdc++.h> using namespace std; #define maxn 500005 int p[maxn]; ]; int start; in ...

  8. Manacher算法 - 求最长回文串的利器

    求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...

  9. Manacher算法——求最长回文子串

    首先,得先了解什么是回文串.回文串就是正反读起来就是一样的,如“abcdcba”.我们要是直接采用暴力方法来查找最长回文子串,时间复杂度为O(n^3),好一点的方法是枚举每一个字符,比较较它左右距离相 ...

随机推荐

  1. coreseek(sphinx)错误:WARNING: attribute 'id' not found - IGNORING

    coreseek(sphinx)错误:WARNING: attribute 'id' not found - IGNORING原因及解决方法 coreseek(sphinx)建立索引时提示错误: WA ...

  2. 如何在Android Studio中使用Gradle发布项目至Jcenter仓库

    简述 目前非常流行将开源库上传至Jcenter仓库中,使用起来非常方便且易于维护,特别是在Android Studio环境中,只需几步配置就可以轻松实现上传和发布. Library的转换和引用 博主的 ...

  3. var a =a || {}

  4. 一个关于导出excel模板的实例

    1 首先jsp页面 点击模板下载,会自动下载模板excel,效果如下 让我们看源码: 1 jsp页面 <div class="tab-pane" id="profi ...

  5. 取消开机window 设备选择

    问题:开机,出现window 设备选择,在win7 页面上会停留几秒.解决方案:win+R 输入 -msconfig 进入引导 把延迟改为3

  6. javascript design patterns

    http://jsdesignpatterns.com/ http://www.joezimjs.com/tag/design-patterns/ http://codecube.net/#archi ...

  7. FileUtils

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

  8. delphi OnMouseLeave 事件不灵敏及解决之道(使用TrackMouseEvent函数进行加强)

    http://topic.csdn.net/t/20020104/09/456913.html CM_MouseLeave消息好象不太灵敏,当鼠标快速移出窗体时,就收不到这个消息,请问大家有什么好办法 ...

  9. Struts2的声明式异常处理

    在struts2应用程序中你还在使用try catch语句来捕获异常么?如果是这样的,那你OUT啦!struts2支持声明式异常处理,可以再Action中直接抛出异常而交给struts2来 处理,当然 ...

  10. Spring MVC学习总结。

    公司项目用的Spring MVC.顺便学习学习. 其实框架并没有想象中的复杂.尤其对于初学者,总觉得SSH是一些很复杂的东西似的.其实对初学者来说能够用这些框架就足够了.在公司里也是,基本功能会用了就 ...