题意:找串的最长回文字串(连续)

题解:manacher版题

一些理解:首位加上任意两个字符是为了判断边界。

     本算法主要是为了

      1.省去奇偶分类讨论。

      2.防止形如aaaaaaa的串使得暴力算法蜕化为n^2;

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
const int maxn = 3e5;;
char s[maxn], str[maxn];
int len1, len2, p[maxn], ans; void init() {
str[] = '$';
str[] = '#';
for (int i = ; i < len1; i++) {
str[i * + ] = s[i];
str[i * + ] = '#';
}
len2 = len1 * + ;
str[len2] = '*';
}
void manacher() {
int id = ,mx = ;
for (int i = ; i < len2; i++) {
if (mx > i)p[i] = min(p[ * id - i], mx - 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;
}
}
} int main() {
while (scanf("%s", s) != EOF) {
len1 = strlen(s);
init();
ans = ;
manacher();
for (int i = ; i < len2; i++) {
ans = max(ans, p[i]);
}
cout << ans- << endl;
}
}

最长回文 HDU - 3068 manacher 模板题的更多相关文章

  1. (最长回文串 模板) 最长回文 -- hdu -- 3068

    http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  2. Palindrome - POJ 3974 (最长回文子串,Manacher模板)

    题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了.   代码如下: ================================================= ...

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

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

  4. 最长回文 HDU 3068 (裸的Manacher)

    直接看代码: ============================================================================================= ...

  5. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  6. 最长回文子串的Manacher算法

    对于一个比较长的字符串,O(n^2)的时间复杂度是难以接受的.Can we do better? 先来看看解法2存在的缺陷. 1) 由于回文串长度的奇偶性造成了不同性质的对称轴位置,解法2要对两种情况 ...

  7. hihocode #1032 : 最长回文子串【manacher】模板题

    题目链接:https://vjudge.net/problem/HihoCoder-1032 manacher算法详解:https://blog.csdn.net/dyx404514/article/ ...

  8. 最长回文子串(Manacher算法)

    回文字符串,想必大家不会不熟悉吧? 回文串会求的吧?暴力一遍O(n^2)很简单,但当字符长度很长时便会TLE,简单,hash+二分搞定,其复杂度约为O(nlogn), 而Manacher算法能够在线性 ...

  9. 51nod1089(最长回文子串之manacher算法)

    题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 题意:中文题诶~ 思路: 我前面做的那道回文子串的题 ...

随机推荐

  1. 使用npm国内镜像

    嫌npm指令速度慢的童鞋可以把npm的源转换成国内的即可提高响应速度: 镜像使用方法(三种办法任意一种都能解决问题,建议使用第1或者第3种,将配置写死,下次用的时候配置还在):1.通过config命令 ...

  2. Images.xcassets 的简单使用

    总结起来Images.xcassets 就这么几句话 (1)以下開始简单整理 进入images.xcassets 右键--->New Image Set 或者点XCode的images.xcas ...

  3. JavaScript-this理解

    javascript this可以绑定到:全局对象,自己定义的对象,用构造函数生成的对象,通过call或者apply更改绑定的对象    1.全局对象  function globalTest(nam ...

  4. [Python]小百合十大爬虫

    国庆几天在家看了几篇关于使用Python来编写网络爬虫的博客,想来自己断断续续学习Python也有几个月了,但一个像样的程序都没有写过,编程能力并没有得到提高,愧对自己花费的时间.很多时候虽然知道什么 ...

  5. 3dmax osg格式导出插件 osgExp OpenSceneGraph Max Exporter

    https://sourceforge.net/projects/osgmaxexp/files/OpenSceneGraph%20Max%20Exporter/

  6. [Shell] Shell 中的算术

    Shell 脚本变量默认是作为字符串处理,而不是数字,这使得在 Shell 脚本做数学运算显得较为复杂.在保持脚本编程规范和更好的算术支持方便,Perl 和 Python 会是更好的选择.但是你仍然可 ...

  7. ibatis 引入多个model

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "- ...

  8. java远程连接access数据库

    本文转载自http://www.voidcn.com/article/p-tlrtkqlp-k.html 1  rmijdbc远程连接access数据库 正常情况下,常用的数据库sql server, ...

  9. C# GIF图片的分解以及合成

    dll下载的地址 http://www.codeproject.com/Articles/11505/NGif-Animated-GIF-Encoder-for-NET   使用主要调用的是Compo ...

  10. 使用kendynet构建异步redis访问服务

    使用kendynet构建异步redis访问服务 最近开始在kendynet上开发手游服务端,游戏类型是生存挑战类的,要存储的数据结构和类型都比较简单,于是选择了用redis做存储,数据类型使用stri ...