给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.
回文就是正反读都是一样的字符串,如aba, abba等
Input输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c...y,z组成的字符串S

两组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是以id为中心,对应最远的回文右边界。所以这种情况p[i]=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 最长回文的更多相关文章

  1. Manacher(hdu3068最长回文)

    浅谈manacher算法 manacher算法是我在网上无意中找到的,主要是用来求某个字符串的最长回文子串. 不过网上的版本还不太成熟,我就修改了下. 不要被manacher这个名字吓倒了,其实man ...

  2. hdu3068最长回文(Manacher算法)

    简单来说这是个很水的东西.有点dp的思想吧.推荐两个博客,很详细. http://blog.csdn.net/xingyeyongheng/article/details/9310555 http:/ ...

  3. HDU3068 最长回文 MANACHER+回文串

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...

  4. 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 ...

  5. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  6. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  7. 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 ...

  8. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace

    CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...

随机推荐

  1. Vue指令学习

    # new Vue({ vue所有的数据都是放到data里面的 # data:{ vue对象的数据 # a:1,对象 # b:[] , # } # methods:{vue对象的方法 # dosomt ...

  2. Solaris10 如何设置空闲ssh连接超时断开

    在ssh的配置文件中有2个参数可以控制空闲连接超时断开.这2个参数是ClientAliveCountMax和ClientAliveInterval. Solaris10上设置空闲ssh连接超时断开的方 ...

  3. jdbcTemplate学习(二)

    前面讲了增加.删除.更新操作,这节讲一下查询. 查询操作: (一)查询一个值(不需要注入参数) queryForObject(String sql, Class<T> requiredTy ...

  4. struts2学习笔记(2)action多个方法的动态调用

    ①在struts.xml中的action添加method <action name="addhelloworld" method="add" class= ...

  5. Emulator PANIC: Could not open: AVD2.3.1

    这是这两年的sdk才需要这样,以前这样根本没错的 在环境变量 里面增加一个系统变量ANDROID_SDK_HOME,值就是当前的系统用户文件夹的位置.比如c:\\Users\xxx(不要加.andro ...

  6. 每天一道算法题(32)——输出数组中第k小的数

    1.题目 快速输出第K小的数 2.思路 使用快速排序的思想,递归求解.若键值位置i与k相等,返回.若大于k,则在[start,i-1]中寻找第k大的数.若小于k.则在[i+1,end]中寻找第k+st ...

  7. 【android】关于自己实现adapter后gridview中item无法被选中的解决方法

    有时候,自己继承实现了baseadapter将其赋给gridview之后,gridview会十分奇怪的无法选中内部的item. 经过仔细研究,我发现是在继承的时候多复写了几个方法,解决方法就是,只保留 ...

  8. springboot启动过程(2)-run方法

    1 springApplication的run run方法主要是用于创造spring容器ConfigurableApplicationContext对象. public ConfigurableApp ...

  9. JavaScript基础笔记集合(转)

    JavaScript基础笔记集合   JavaScript基础笔记集合   js简介 js是脚本语言.浏览器是逐行的读取代码,而传统编程会在执行前进行编译   js存放的位置 html脚本必须放在&l ...

  10. DOM 操作属性

    DOM操作就是针对对象的操作 先写一个按钮,<input tupe="button" value=""  id="id">  这 ...