ACM题目————最长回文串
Description
回文就是正反读都是一样的字符串,如aba, abba等
Input
两组case之间由空行隔开(该空行不用处理)
字符串长度len <= 110000
Output
Sample Input
abab
Sample Output
3
用manacher算法求最长回文串长度。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <string>
#include <queue>
#define INF 10000001 using namespace std;
const int maxn = *;
char str[maxn];
int p[maxn]; void manacher(char *s, int len){
p[] = ;
int Max = , d= ;
for(int i=; i<len; i++){
p[i] = Max > i ? min(p[d*-i],Max-i): ;
while( s[i+p[i]] == s[i-p[i]]) p[i] ++ ;
if( i + p[i] > d + p[d] ){
d = i ;
Max = i + p[i] ;
}
}
} int main(){
while( ~ scanf("%s",str) ){
int len = strlen(str);
for(int i=len; i>=; i--){
str[(i<<)+] = '#' ;
str[(i<<)+] = str[i] ;
}
str[] = '*' ;
len = len * + ;
manacher(str,len);
int ans = ;
for(int i=; i<len; i++){
ans = max(p[i]-,ans);
}
printf("%d\n",ans);
}
return ;
}
ACM题目————最长回文串的更多相关文章
- 字符串的最长回文串:Manacher’s Algorithm
题目链接:Longest Palindromic Substring 1. 问题描述 Given a string S, find the longest palindromic substring ...
- Manacher's Algorithm 马拉车算法(求最长回文串)
作用:求一个字符串中的最长子串,同时还可以求所有子串的长度. 题目链接: https://vjudge.net/contest/254692#problem/B 最长回文串长度的代码: int Man ...
- (最长回文串 模板) 最长回文 -- hdu -- 3068
http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- Hdu 3294 Girls' research (manacher 最长回文串)
题目链接: Hdu 3294 Girls' research 题目描述: 给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.... ...
- Leetcode0005--Longest Palindromic Substring 最长回文串
[转载请注明]http://www.cnblogs.com/igoslly/p/8726771.html 来看一下题目: Given a string s, find the longest pali ...
- Manacher(输出最长回文串及下标)
http://acm.hdu.edu.cn/showproblem.php?pid=3294 Girls' research Time Limit: 3000/1000 MS (Java/Others ...
- Manacher(最长回文串)
http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...
- HDU 3068 最长回文 (Manacher最长回文串)
Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等 Input 输 ...
- leetcode 每日签到 409. 最长回文串
题目: 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: ...
随机推荐
- gradient color
http://www.cnblogs.com/YouXianMing/p/3793913.html layer 不能自动autolay, 只能在viewDidLayout里面设置宽度 - (void) ...
- css 字数超过一行显示省略号
display:block;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;
- JavaBean 动作元素事例
JavaBean.jsp JavaBeanSuccess.jsp Type类 效果
- session 实现保存用户信息
index.jsp <body> <div style="margin: 0 auto; width: 500px; text-align: center;"&g ...
- 转:NodeJS、NPM安装配置步骤
1.windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://nodejs.org/),便可以看到下载页面. 2.下载完 ...
- prezi破解教程
http://www.joenchen.com/archives/998 http://www.joenchen.com/archives/945 Prezi Desktop 4.7.5免注册无时间限 ...
- C#: XML Serializer
这里主要讲如何将一个class序列化为一个string.如下一个class: public class OrderedItem { private string itemName; private s ...
- SpringMvc的数据绑定流程
在SpringMvc中会将来自web页面的请求和响应数据与controller中对应的处理方法的入参进行绑定,即数据绑定.流程如下: -1.SpringMvc主框架将ServletRequest对象及 ...
- const修饰
const int A() //const // ====>int A(const this) { //观点1:const是修饰a,但是通过测试,我们发现,b++也不能编译通过 //这说明:co ...
- 04---Net基础加强
字符串常用方法: 属性: Length获取字符串中字符的个数 IsNullOrEmpty() 静态方法,判断为null或者为“” ToCharArray() 将string转换为char[] To ...