Palindrome(最长回文串manacher算法)O(n)
Description
A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.
The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!".
If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
Input
Output
Sample Input
abcbabcbabcba
abacacbaaaab
END
Sample Output
Case 1: 13
Case 2: 6
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int maxn=+;
//manacher算法
const int LEN=maxn;
const int N=LEN*;
int p[N];
char str[LEN],tmp[N];
//p[i]表示以str[i]为中心的回文往右延伸的 最长长度
void manacher(char* str, int* p)
{
int n=strlen(str), i, id, mx;
for(p[mx=id=]=i=; i<n; i++)
{
p[i]=mx>i?min(p[*id-i], mx-i+):;
while(i+p[i]<n&&i-p[i]>=&&str[i+p[i]]==str[i-p[i]]) p[i]++;
if(mx<i+p[i]-)
{
id=i;
mx=i+p[i]-;
}
}
}
//求str的最长回文的长度
int maxPalindrome(char* str)
{
int ans=;
int i, n;
for(i=, n=strlen(str); i<n; i++)
{
tmp[*i]='#';
tmp[*i+]=str[i];
}
tmp[*i]='#';
tmp[*i+]='\0';
n=n*+;
manacher(tmp, p);
for(i=; i<n; i++)
{
if(ans < p[i]-)
{
ans = p[i]-;
}
}
return ans;
}
int main()
{
int i=;
for(i=;; i++)
{
scanf("%s",str);
char s[]= {'E','N','D','\0'};
if(strcmp(str,s)==)
break;
else
printf("Case %d: %d\n",i,maxPalindrome(str));
}
return ;
}
Palindrome(最长回文串manacher算法)O(n)的更多相关文章
- luoguP4555 [国家集训队]最长双回文串 manacher算法
不算很难的一道题吧.... 很容易想到枚举断点,之后需要处理出以$i$为开头的最长回文串的长度和以$i$为结尾的最长回文串的长度 分别记为$L[i]$和$R[i]$ 由于求$R[i]$相当于把$L[i ...
- 最长回文---hdu3068 (回文串 manacher 算法模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题意很清楚:就是求一个串s的子串中最长回文串的长度:这类题用到了manacher算法 #incl ...
- 【BZOJ2565】最长双回文串 (Manacher算法)
题目: BZOJ2565 分析: 首先看到回文串,肯定能想到Manacher算法.下文中字符串\(s\)是输入的字符串\(str\)在Manacher算法中添加了字符'#'后的字符串 (构造方式如下) ...
- hdu 3068 最长回文 (Manacher算法求最长回文串)
参考博客:Manacher算法--O(n)回文子串算法 - xuanflyer - 博客频道 - CSDN.NET 从队友那里听来的一个算法,O(N)求得每个中心延伸的回文长度.这个算法好像比较偏门, ...
- lintcode最长回文子串(Manacher算法)
题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...
- 九度OJ 1528 最长回文子串 -- Manacher算法
题目地址:http://ac.jobdu.com/problem.php?pid=1528 题目描述: 回文串就是一个正读和反读都一样的字符串,比如"level"或者"n ...
- 最长回文字串——manacher算法
时间复杂度:O(n) 参考:https://segmentfault.com/a/1190000003914228 1.问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字 ...
- 37:密码截取(回文串manacher算法)
题目描述:Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解.比如 ...
- HDU 3068:最长回文(Manacher算法)
http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的 ...
随机推荐
- HDU 3729 二分匹配 反向匹配
题意: 给定 n个学生 说的 自己 考试排名的 可能范围 确定最多几个人说真话 如果有多种答案,输出字典序最大的那种( 要求字典序最大,所以solve中从最大字典序开始匹配) 思路: 题目给定 点 ...
- 11.PHP 教程_PHP Switch 语句
switch 语句用于根据多个不同条件执行不同动作. PHP Switch 语句 如果您希望有选择地执行若干代码块之一,请使用 switch 语句. 语法 switch (n) { case labe ...
- Android UiAutomator 自动化测试环境搭建---新手1
1.首先需要准备的工具有 1.java jdk 2. android开发工具 adt 3.ant 安装包(如果下载adt里面有) 2.首先安装java环境,jdk这个百度就可以了. 3.android ...
- Vistual Studio 2010 调试无法进断点
系统是2003出现的问题 win8就没事 打sp1 补丁就行
- Asp.net Role manager tutorial
It is very useful in .net we can user framework provided role manager, and easily configure in Web.C ...
- [转]IOS 中文排序
转自:http://www.cnblogs.com/syxchina/archive/2012/10/11/2720257.html 1 原因 Ios默认使用utf-8格式编码,所以中文在IOS中默认 ...
- 图中两点间路径为l的数目
用矩阵G表示图的邻接阵. G2中的元素就是两点间路径为2的路径数,同理G3就是两点间路径为3的路径数目. 并且此结论同样适用于有向图. 甚至,此结论适用于有权图,只是算出来的不再是路径数,而是各条路径 ...
- QString类的使用(无所不包,极其方便)
Qt的QString类提供了很方便的对字符串操作的接口. 使某个字符填满字符串,也就是说字符串里的所有字符都有等长度的ch来代替. QString::fill ( QChar ch, int size ...
- nginx启动、开机自启动、重启、关闭
yum -y install nginx # yum info nginx Loaded plugins: fastestmirror Loading mirror speeds from cach ...
- 使用回调接口实现ActiveX控件和它的容器程序的通讯
本文阅读基础:有一定的C++基础知识(了解继承.回调函数),对MFC的消息机制有一定了解,对COM的基础知识有一定了解,对ActiveX控件有一定了解. 一. 前言 ActiveX控件和它的容器程序如 ...