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. day2练习题

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...

  2. 重装Ubuntu系统并配置开发环境

    安装 Ubuntu 并配置开发环境 写一篇文章详细记录下来所有的过程,以便以后参考. 安装前的准备 备份所有代码和配置文件 备份下载的各类文件 Ubuntu 安装 下载安装 Ubuntu14.04,下 ...

  3. 火星02坐标转换为WGS84坐标

    import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import jav ...

  4. Rss web 工具 大对比

    今天终于神受不了 feedly的链接死掉了..有时候挂代理就好了..但是麻烦. 于是: AOL reader Digg  reader feedly 对比下.使用了一天 1.feedly 优: 效果最 ...

  5. KV总结

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

  6. NtQuerySystemInformation的使用(提供50余种信息)

    今天,我们主要讨论的是一个函数NtQuerySystemInformation(ZwQuerySystemInformation).当然,你不要小看这么一个函数,它却为我们提供了丰富的系统信息,同时还 ...

  7. Delphi 和 DFM

    Delphi et les DFM Depuis la toute première version de Delphi, celui-ci intègre des fichiers à l'exte ...

  8. 想做Android Wear开发?你得先搞明白这四件事

    手环和手表的腕上穿戴之争,随着Apple Watch发布和Android Wear不断完善而告一段落.尽管续航上略有缺陷,但手表以其类似手机可扩展的生态环境赢得了众多巨头的支持. Google曾透露, ...

  9. android利用反射通过代码收缩通知栏

    最近有个需求,点击通知栏RemoteView中的按钮后要收起通知栏,系统默认是不自动收起的,不过没有找到公开的API可以控制通知栏. 在android.app.StatusBarManager里提供了 ...

  10. zabbix 参数说明

    <pre name="code" class="html">数据采集的工作模式可以分为被动模式(服务器端到客户端采集数据) 主动模式(客户端主动上报 ...