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, ...
随机推荐
- RabbitMQ 消息队列 应用
安装参考 详细介绍 学习参考 RabbitMQ 消息队列 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. M ...
- 安装 MongoDB。
1.安装 MongoDB. 1.为软件包管理系统导入公钥. Ubuntu 软件包管理工具为了保证软件包的一致性和可靠性需要用 GPG 密钥检验软件包.使用下列命令导入 MongoDB 的 GPG 密钥 ...
- div的作用
<div></div>主要是用来设置涵盖一个区块为主,所谓的区块是包含一行以上的数据,所以在<div></div>的开始之前与结束后,浏览都会自动换行, ...
- xcode 编译错误找不到 libz.dylib
图片对应的是libxml2.dylib (libz.dylib 遇到的编译错误跟这个类似) 解决方法是在引入库的地方调整原先 比如libz.dylib 的目录: ================== ...
- hive like 模糊匹配
类似: 在MYSQL里面我们可以这样的执行SQL select a.Community,a.PID,b.spidertime,b.comm,b.showings,b.room from lianjia ...
- fluent仿真数值错误
- C++之deque
deque(包含头文件#include<deque>)由若干段连续空间串接而成,一旦有必要在deque的头部或尾端增加新的空间,便配置一段定量连续的空间,串接在deque的头部或尾端.de ...
- Ajax笔记(一)
Ajax三步骤: Asynchronous Javascript And XML 1.运用HTML和CSS实现页面,表达信息: 2.运用XMLHttpRequest和web服务器进行数据的异步交换: ...
- Action层, Service层 和 Dao层的功能区分
Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO ...
- 51NOD 1371填数字
传送门 分析 此题关键在于想出dp[i][j][k]代表考虑到第i行,还能放1的的共有j列,还能放2的共有k行.之后就枚举每一行是没有还是1个1还是2个1还是1个2,然后转移即可. 代码 #inclu ...