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. xml和html之间相互转换

    一.xml转换html xml+xslt是典型的数据与表现分离的设计方式.当然,你可以直接转换成HTML,但是如果你要进行整体变化的时候,XML+XSLT的优势就体现出来了.同样的数据,因为你已经有X ...

  2. NodeJs开发学习目录

    1.Nodejs基本概念及Nodejs.npm安装测试[2014-06-06] 2.开发工具简介(主要介绍Sublime Text使用) [2014-06-06] 3.Sublime text插件安装 ...

  3. jquery $(this).attr $(this).val方法使用介绍--useful

    $(this).attr(key); 获取节点属性名的值,相当于getAttribute(key)方法,本文整理了一些相关的示例,感兴趣的朋友可以参考下 $(this).attr(key); 获取节点 ...

  4. js/bat批处理调用谷歌浏览器chrome批量打开网页测试web性能

    批处理批量打开网页 其实用java就可以搞定,但是这么一个轻巧的测试,js或者bat批处理去一次性打开几百个网页测试一下页面没必要上java 两者的区别,js的话,只能打开多个浏览器实例,不方便查看效 ...

  5. KV总结

    今天没事又重新写了一遍.很多注释是自己犯糊涂后来又终于跨过去的备忘. // ImgEff.js function ImgEff(div,time){ //构造函数,需要传入参数div的id和时间 // ...

  6. eclipse中tomcat内存溢出问题,报PermGen space

    场景 最近在eclipse中的tomcat服务器下放三个不同的应用程序,其中两个应用程序用到了各自的第三方jar包.刚开始时把这三个应用程序分别部署到各自的tomcat服务器运行,没问题.后来想通过第 ...

  7. 【HDOJ】3309 Roll The Cube

    BFS,考虑一球进洞仅一球滚动以及两球重叠的情况即可. /* 3309 */ #include <iostream> #include <queue> #include < ...

  8. 【HDOJ】2333 Assemble

    二分+贪心策略.其中注释处很重要. #include <iostream> #include <cstdio> #include <cstring> #includ ...

  9. TCP/IP学习(四)TCP缓冲区大小及限制(转)

    链接来自:http://blog.csdn.net/ysu108/article/details/7764461 这个问题在前面有的部分已经涉及,这里在重新总结下.主要参考UNIX网络编程. (1)数 ...

  10. 代码审查工具之PMD操作指南

    上周客户要求对OA系统的代码质量进行了一个整体审查,并且要出一份报告给领导. 为此花了半天时间把代码审查工具PMD琢磨了下,现将具体操作步骤描述如下,以供大家参考! 1 前言 质量是衡量一个软件是否成 ...