kuangbin专题十六 KMP&&扩展KMP HDU3068 最长回文
回文就是正反读都是一样的字符串,如aba, abba等
两组case之间由空行隔开(该空行不用处理)
字符串长度len <= 110000Output每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度.
Sample Input
aaaa abab
Sample Output
4
3 马拉车裸题。
再次学习马拉车 第一步:字符串预处理,保证长度都为奇数。
明确id,mx的含义 id为最远回文对应的中心位置,mx为最远回文位置
用p[i]记录以i为中心的最大回文半径 第二步:求p[i] 分两种情况:
一、mx > i

①,mx-i>p[i] 如图所示:红色部分为i的回文范围。i和j关于id对称。说明p[i]=p[j]=p[2*id-i] ②,mx-i<=p[i]

如图所示,红色为中心为i的回文范围,如果i+p[i]>mx, 那么只能说明 mx-i 这一段是可以由之前的推出来的
二、mx<=i 说明根本不知道以后的情况,只能p[i] = 1
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
char s[],snew[];
int p[]; int manacher(char* s) {
int l=;
snew[l++]='$';
snew[l++]='#';
for(int i=;s[i];i++) {
snew[l++]=s[i];
snew[l++]='#';
}
snew[l]='\0';
int id=,mx=,maxlen=-;
for(int i=;i<l;i++) {
p[i]=i<mx?min(p[*id-i],mx-i):;
while(snew[i+p[i]]==snew[i-p[i]]) p[i]++;
if(i+p[i]>mx) {
mx=i+p[i];
id=i;
}
if(p[i]>maxlen) maxlen=p[i]-;
}
return maxlen;
} int main() {
while(~scanf("%s",s)) {
printf("%d\n",manacher(s));
}
return ;
}
kuangbin专题十六 KMP&&扩展KMP HDU3068 最长回文的更多相关文章
- Manacher(hdu3068最长回文)
浅谈manacher算法 manacher算法是我在网上无意中找到的,主要是用来求某个字符串的最长回文子串. 不过网上的版本还不太成熟,我就修改了下. 不要被manacher这个名字吓倒了,其实man ...
- hdu3068最长回文(Manacher算法)
简单来说这是个很水的东西.有点dp的思想吧.推荐两个博客,很详细. http://blog.csdn.net/xingyeyongheng/article/details/9310555 http:/ ...
- HDU3068 最长回文 MANACHER+回文串
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...
- kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
- kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
随机推荐
- webpack3.x看这个就够了
本文介绍webpack3.x的使用 说明,本文前后连贯性很强,建议从头往后看 目录 开始 css文件打包 image文件打包 字体文件打包 json文件打包 csv文件和xml文件打包 多入口文件打包 ...
- ansible for devops 读书笔记第二章Ad-Hoc Commands
参数 参数 说明 -a ‘Arguments’, —args=’Arguments’ 命令行参数 -m NAME, —module-name=NAME 执行模块的名字,默认使用 command 模块, ...
- Jquery前端选择器
----------------------祖先后代选择器------------------------------ 1.祖先 后代:根据一个元素可以取得指定的所有子元素(不管中间有多少后代)$(& ...
- hibernate学习笔记(3)hibernate常用配置以及session对象
更改hibernate.cfg.xml的内容,常用配置有: <!-- 把hibernate运行时的SQL语句显示到控制台 --> <property name="sho ...
- 【WebRTC】简介
WebRTC 名称源自网页实时通信(英语:Web Real-Time Communication)的缩写,是一个支持网页浏览器进行实时语音对话或视频对话的API.它于2011年6月1日开源并在Goog ...
- [poj1410]Intersection
题目大意:求线段与实心矩形是否相交. 解题关键:转化为线段与线段相交的判断. #include<cstdio> #include<cstring> #include<al ...
- strstr()查找函数,strchr(),strrchr(),stristr()/strpos(),strrpos()查找字符串位置
在一个较长的字符串这查找匹配的字符串或字符,其中strstr()和strchr()是完全一样的. 例: echo strstr('why always you','you'); 输出: you 如果为 ...
- linq 初步认识
linq to sql 类 介绍: linq如果不能用的话 重装一下vs就好了 LINQ,语言集成查询(Language Integrated Query)是一组用于c#和Visual Basic语言 ...
- IO流框架关系总结(关系图)
字节流和字符流关系图 打印流和序列化流关系图
- Python 黑客 --- 001 UNIX口令破解机
Python 黑客 实战:UNIX口令破解机 使用的系统:Ubuntu 14.04 LTS Python语言版本:Python 2.7.10 V crypt 库是Python内置的库.在UNIX系统使 ...