和上一题一样,不过这题只是要求最长回文子串的长度

在此采用了非常好用的Manacher算法

据说还是O(n) 的效率QAQ

详细用法参考了上篇博客的参考资料,这两天有空学习一下~

Source code:

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ; char str[M];//start from index 1
int p[M];
char s[M];
int n; void kp(){
int i;
int mx = ;
int id;
for(i = ; i < n; ++i){
if( mx > i )
p[i] = Min( p[*id-i], p[id]+id-i );
else
p[i] = ;
for(; str[i+p[i]] == str[i-p[i]]; p[i]++)
;
if( p[i] + i > mx ){
mx = p[i] + i;
id = i;
}
}
} void pre(){
int i,j,k;
n = strlen(s);
str[] = '$';
str[] = '#';
for(i = ; i < n; ++i){
str[i* + ] = s[i];
str[i* + ] = '#';
}
n = n* + ;
str[n] = ;
} int main(){
int T,_=, t, i, ans;
while(EOF != scanf("%s", s)){
ans = ;
pre();
kp();
for(i = ; i < n; ++i)
checkmax(ans, p[i]);
printf("%d\n", ans-);
}
return ;
}

HDU 3068 最长回文 【最长回文子串】的更多相关文章

  1. [hdu 3068] Manacher算法O(n)最长回文子串

    一个不错的讲解:https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/01.05.md # ...

  2. hdu 3068 最长回文 manacher算法(视频)

    感悟: 首先我要Orz一下qsc,我在网上很难找到关于acm的教学视频,但偶然发现了这个,感觉做的很好,链接:戳戳戳 感觉这种花费自己时间去教别人的人真的很伟大. manacher算法把所有的回文都变 ...

  3. 最长回文子串(百度笔试题和hdu 3068)

    版权所有.所有权利保留. 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/17123559 求一个字符串的最长回文子串.注 ...

  4. hdu 3068 最长回文(manachar求最长回文子串)

    题目连接:hdu 3068 最长回文 解题思路:通过manachar算法求最长回文子串,如果用遍历的话绝对超时. #include <stdio.h> #include <strin ...

  5. HDU - 3068 最长回文(manacher)

    HDU - 3068 最长回文 Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Subm ...

  6. hdu 3068 最长回文 (Manacher算法求最长回文串)

    参考博客:Manacher算法--O(n)回文子串算法 - xuanflyer - 博客频道 - CSDN.NET 从队友那里听来的一个算法,O(N)求得每个中心延伸的回文长度.这个算法好像比较偏门, ...

  7. 最长回文 HDU - 3068(马拉车算法)

    Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入 ...

  8. HDU - 3068 最长回文manacher马拉车算法

    # a # b # b # a # 当我们遇到回判断最长回文字符串问题的时候,若果用暴力的方法来做,就是在字符串中间添加 #,然后遍历每一个字符,找到最长的回文字符串.那么马拉车算法就是在这个基础上进 ...

  9. 【BZOJ2565】最长双回文串(回文树)

    [BZOJ2565]最长双回文串(回文树) 题面 BZOJ 题解 枚举断点\(i\) 显然的,我们要求的就是以\(i\)结尾的最长回文后缀的长度 再加上以\(i+1\)开头的最长回文前缀的长度 至于最 ...

  10. HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)

    HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. 过程需要类型为 'ntext/nchar/nvarchar' 的参数 '@statement'

    declare   @Sql Nvarchar(4000)   SET @Sql = ' select * from a_table '要么这样, 要不然在执行存储过程变量前加N'' ALTER PR ...

  2. Array.Add () and += in PowerShell

    $newArray = @() $newArray.Add("Hello") If I create a new array, and using the method Add() ...

  3. 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录

    1 刚进入该界面的时候发生错误,原因是 list="roles"中的这个集合是空的,导致错误 解决办法很简单,不能让list为空 2 刚进入该界面的时候list是有数据的,当点击提 ...

  4. PROTEL99生成GERBER的操作说明

    GBL BOTTOM LAYER(底层布线图)GBO BOTTOM OVERLAYER(底层丝印层)GBP BOTTOM PASTE LAYER(底层锡膏层)GBS BOTTOM SOLDER MAS ...

  5. Shot(三分)

    Shot Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. 关于SpringMVC中找不到<mvc:resources/>标签的解决办法

    在springMVC中我们经常会用到<mvc:resources/>标签,但是有些编辑器中的schema过于陈旧.导致找不到<mvc:resources/>标签. 经过试验,有 ...

  7. oracle 中的select ...connect by prior ...start with 及(+)的用法

    1.select ...connect by prior ...start with的用法: select ... from <tablename> where <condition ...

  8. iOS Dev (67) 单例的实现

    iOS Dev (67) 单例的实现 博客:http://blog.csdn.net/prevention 作者:大锐哥 摘自:Learn iPhone and iPad cocos2d Game D ...

  9. VS2008编译iconv静态链接库

    iconv是将一种编码格式转换为还有一种编码格式的开源库,比如能够把Windows环境下通用的ASCii(中文是GB2312)编码转换为国际通用的Unicode编码 iconv最新版本号仅仅支持Min ...

  10. HDU 1222 Wolf and Rabbit(gcd)

    HDU 1222   Wolf and Rabbit   (最大公约数)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...