http://poj.org/problem?id=3974

模板题,Manacher算法主要利用了已匹配回文串的对称性,对前面已匹配的回文串进行利用,使时间复杂度从O(n^2)变为O(n)。

https://www.cnblogs.com/xiaoningmeng/p/5861154.html 详细解释

https://www.zhihu.com/question/30226229 这是复杂度O(n)的解释

代码

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<map>
#include<ctime>
using namespace std;
const int maxn=;
char c[maxn];
char s[maxn*];
int p[maxn*];
int len;
void init(){
len=strlen(c);
int j=;
s[]='$';s[]='#';
for(int i=;i<len;i++){
s[++j]=c[i];
s[++j]='#';
}s[++j]='\0';
len=j;
}
int Manacher(){
init();int ans=;
int id,mx=;
for(int i=;i<len;i++){
if(i<mx) p[i]=min(p[*id-i],mx-i);
else p[i]=;
while(s[i-p[i]]==s[i+p[i]])
p[i]++;
if(mx<i+p[i]){
id=i;
mx=i+p[i];
}
ans=max(ans,p[i]);
}
return ans-;
}
int main(){
for(int i=;;++i){
scanf("%s",&c);
if(c[]=='E')break;
printf("Case %d: %d\n",i,Manacher());
}
return ;
}

POJ 3974 Palindrome 字符串 Manacher算法的更多相关文章

  1. POJ 3974 - Palindrome - [字符串hash+二分]

    题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...

  2. POJ 1159 Palindrome(字符串变回文:LCS)

    POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...

  3. ●POJ 3974 Palindrome(Manacher)

    题链: http://poj.org/problem?id=3974 题解: Manacher 求最长回文串长度. 终于会了传说中的马拉车,激动.推荐一个很棒的博客:https://www.61mon ...

  4. POJ 3974 Palindrome

    D - Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  5. POJ 3974 Palindrome(最长回文子串)

    题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...

  6. Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp

    题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...

  7. 第5题 查找字符串中的最长回文字符串---Manacher算法

    转载:https://www.felix021.com/blog/read.php?2040 首先用一个非常巧妙的方式,将所有可能的奇数/偶数长度的回文子串都转换成了奇数长度:在每个字符的两边都插入一 ...

  8. Palindrome POJ - 3974 (字符串hash+二分)

    Andy the smart computer science student was attending an algorithms class when the professor asked t ...

  9. poj 3974 Palindrome (manacher)

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 12616   Accepted: 4769 Desc ...

随机推荐

  1. js中字符串和json数组的相互转换

    //示例 var a={"name":"tom","sex":"男","age":"24& ...

  2. 去除IE10+上文本框巨丑无比的删除图标以及显示密码图标

    去除IE10+上文本框巨丑无比的删除图标以及显示密码图标 IE浏览器总是让人喜欢让人厌,在最新的IE浏览器(IE10+)上使用表单时,文本框内后面会出现很巨丑无比的“删除图标”以及“显示密码图标”,如 ...

  3. 学习Python函数笔记之二(内置函数)

    ---恢复内容开始--- 1.内置函数:取绝对值函数abs() 2.内置函数:取最大值max(),取最小值min() 3.内置函数:len()是获取序列的长度 4.内置函数:divmod(x,y),返 ...

  4. Django 1.10中文文档-第一个应用Part7-自定义管理站点

    开发第一个Django应用,Part7 本教程上接Part6.将继续完成这个投票应用,本节将着重讲解如果用Django自动生成后台管理网站. 自定义管理表单 通过admin.site.register ...

  5. openjudge-NOI 2.6-1768 最大子矩阵

    题目链接:http://noi.openjudge.cn/ch0206/1768/ 题解: 如果用O(n4)的算法肯定会炸,需要压缩掉一维的空间,只需要简单加和就好啦 例如,我们要对样例中第2-4行D ...

  6. spring data jpa条件分组查询及分页

    原book对象 package com.shaying.domain; import javax.persistence.Column; import javax.persistence.Entity ...

  7. CentOS6.9下安装MariaDB10.2.11

    yum groupinstall -y "Development Tools" yum install -y cmake openssl-devel zlib-devel yum ...

  8. Jmeter------将JDBC Request的查询结果作为另一个接口的请求参数

    一.前言 jmeter已配置连接成功数据库,不会的可查看:https://www.cnblogs.com/syw20170419/p/9832402.html 二.需求 将JDBC Request的r ...

  9. fastdfs5.11+centos7.2 按照部署(二)【转载】

    https://files.cnblogs.com/files/xiaojf/nginx-1.12.0.tar.gz https://files.cnblogs.com/files/xiaojf/li ...

  10. [实战]MVC5+EF6+MySql企业网盘实战(23)——文档列表

    写在前面 上篇文章实现了图片列表,这篇文章实现文档列表将轻车熟路,因为逻辑基本相似,只是查询条件的不同.这里将txt,doc,docx,ppt,pptx,xls,xlsx的文件都归为文档列表中. 系列 ...