POJ 3974 Palindrome 字符串 Manacher算法
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算法的更多相关文章
- POJ 3974 - Palindrome - [字符串hash+二分]
题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
- ●POJ 3974 Palindrome(Manacher)
题链: http://poj.org/problem?id=3974 题解: Manacher 求最长回文串长度. 终于会了传说中的马拉车,激动.推荐一个很棒的博客:https://www.61mon ...
- POJ 3974 Palindrome
D - Palindrome Time Limit:15000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 3974 Palindrome(最长回文子串)
题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...
- Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp
题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...
- 第5题 查找字符串中的最长回文字符串---Manacher算法
转载:https://www.felix021.com/blog/read.php?2040 首先用一个非常巧妙的方式,将所有可能的奇数/偶数长度的回文子串都转换成了奇数长度:在每个字符的两边都插入一 ...
- Palindrome POJ - 3974 (字符串hash+二分)
Andy the smart computer science student was attending an algorithms class when the professor asked t ...
- poj 3974 Palindrome (manacher)
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 12616 Accepted: 4769 Desc ...
随机推荐
- 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)
题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...
- python3爬虫.4.下载煎蛋网妹子图
开始我学习爬虫的目标 ----> 煎蛋网 通过设置User-Agent获取网页,发现本该是图片链接的地方被一个js函数代替了 于是全局搜索到该函数 function jandan_load_im ...
- 简单的企业会议管理cms后台模板——后台
链接:http://pan.baidu.com/s/1eRAVAka 密码:olr1
- 福建工程学院寒假作业第一周G题
涨姿势题1 TimeLimit:1000MS MemoryLimit:128000KB 64-bit integer IO format:%lld 涨姿势题就是所谓的优化题,在组队赛中,队伍发现 ...
- Struts访问servletAPI方式
1.原理
- 浅谈iOS多线程
浅谈iOS多线程 首先,先看看进程和线程的概念. 图1.1 这一块不难理解,重点点下他们的几个重要区别: 1,地址空间和资源:进程可以申请和拥有系统资源,线程不行.资源进程间相互独立,同一进程的各线程 ...
- S3C6410 SPI全双工读写流程分析(原创)【转】
转自:http://blog.csdn.net/hustyangju/article/details/21165721 原创博文,知识共享!转载请注明出处:http://blog.csdn.net/h ...
- kvm 简单了解
网络: *主机(装有ESX的PC服务器)简称host,虚拟机简称guest *Host的一个或多个网卡组成一个虚拟交换机,虚拟交换机上创建端口组label,端口组指定vlan tag,虚拟机指定网络标 ...
- centos7安装lamp
一.准备工作 1. 下载并安装CentOS7.2,配置好网络环境,确保centos能上网,可以获取到yum源. centos7.2的网络配置: vim /etc/sysconfig/network ...
- 11.python3标准库--使用进程、线程和协程提供并发性
''' python提供了一些复杂的工具用于管理使用进程和线程的并发操作. 通过应用这些计数,使用这些模块并发地运行作业的各个部分,即便是一些相当简单的程序也可以更快的运行 subprocess提供了 ...