POJ (Manacher) Palindrome
多敲几个模板题,加深一下对Manacher算法的理解。
这道题给的时间限制15s,是我见过的最长的时间的了。看来是为了让一些比较朴素的求最大回文子串的算法也能A过去
Manacher算法毕竟给力,运行时间200+MS
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
char s1[maxn], s2[maxn * ];
int p[maxn * ]; void Init(void)
{
s2[] = '$', s2[] = '#';
int j = ;
for(int i = ; s1[i] != '\0'; ++i)
{
s2[j++] = s1[i];
s2[j++] = '#';
}
s2[j] = '\0';
} void manacher(char s[])
{
int id, mx = ;
p[] = ;
for(int i = ; s[i] != '\0'; ++i)
{
if(mx > i)
p[i] = min(p[id*-i], mx-i);
else
p[i] = ;
while(s[i + p[i]] == s[i - p[i]])
++p[i];
if(i + p[i] > mx)
{
mx = i + p[i];
id = i;
}
}
} int getans(void)
{
int ans = ;
for(int i = ; s2[i] != '\0'; ++i)
ans = max(ans, p[i] - );
return ans;
} int main(void)
{
#ifdef LOCAL
freopen("3974in.txt", "r", stdin);
#endif int kase = ;
while(scanf("%s", s1) != EOF)
{
if(s1[] == 'E') break;
Init();
manacher(s2);
printf("Case %d: %d\n", kase++, getans());
}
return ;
}
代码君
POJ (Manacher) Palindrome的更多相关文章
- 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 字符串 Manacher算法
http://poj.org/problem?id=3974 模板题,Manacher算法主要利用了已匹配回文串的对称性,对前面已匹配的回文串进行利用,使时间复杂度从O(n^2)变为O(n). htt ...
- poj 3974 Palindrome (manacher)
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 12616 Accepted: 4769 Desc ...
- POJ 3974 Palindrome
D - Palindrome Time Limit:15000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- OpenJudge/Poj 1159 Palindrome
1.链接地址: http://bailian.openjudge.cn/practice/1159/ http://poj.org/problem?id=1159 2.题目: Palindrome T ...
- POJ 3974 Palindrome(最长回文子串)
题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...
- POJ 1159 Palindrome(最长公共子序列)
Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...
- POJ 3974 - Palindrome - [字符串hash+二分]
题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...
随机推荐
- js函数延迟执行
function delay(value){ //全局变量保存当前值 window._myTempDalayValue = value; setTimeout(function(){ //延时之后与全 ...
- Java 8怎么了之二:函数和原语
[编者按]本文作者为专注于自然语言处理多年的 Pierre-Yves Saumont,Pierre-Yves 著有30多本主讲 Java 软件开发的书籍,自2008开始供职于 Alcatel-Luce ...
- virtualenv 环境下 Nginx + Flask + Gunicorn+ Supervisor 搭建 Python Web
在这篇文章里,我们将搭建一个简单的 Web 应用,在虚拟环境中基于 Flask 框架,用 Gunicorn 做 wsgi 容器,用 Supervisor 管理进程,然后使用 Python 探针来监测应 ...
- (转)8 reviews about de novo genome assembly
转自:http://dskernel.blogspot.com/2012/04/8-reviews-about-de-novo-genome-assembly.html 8 reviews about ...
- iOS-CALayer图片淡入淡出动画
]; } - (.f; CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:, , ...
- IDEA开发spark本地运行
1.建立spakTesk项目,建立scala对象Test 2.Tesk对象的代码如下 package sparkTest /** * Created by jiahong on 15-8-2. */ ...
- Android NDK引用预编译的动态链接库
NDK里有个例子: android-ndk-r10/samples/module-exports/jni一看就懂了 ———————————————————————————– 从r5版本开始,就支持预编 ...
- http://www.cnblogs.com/TankXiao/p/4018219.html
http://www.cnblogs.com/TankXiao/p/4018219.html
- hadoop 1 testcase运行方法
转入hadoop2.0后,逐渐忘记了之前做testcase运行的方法,记录一下: ant -Dtestcase=Test*** 如果只运行core包得testcase可以 an ...
- 关于WII光枪定位的设计(转)
方法1. 简单1 LED方法 这是一个很忽悠的方法,把LED看成是屏幕中心,把光枪摄像头的视野范围看作是屏幕范围. 假设WII枪头摄像头的数据范围为[0,1024]*[0,768],显示器屏幕分辨率为 ...