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 ...
随机推荐
- javascript实现数据结构:串--定长顺序存储表示以及kmp算法实现
串(string)(或字符串)是由零个或多个字符组成的有限序列.串中字符的数目称为串的长度.零个字符的串称为空串(null string),它的长度为零. 串中任意个连续的字符组成的子序列称为该串的子 ...
- cf div2 236 D
D. Upgrading Array time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- SQL语句AND 和 OR执行的优先级
例句: ) FROM RT_CUSTALLOCRESULT WHERE REGDATE BETWEEN '2014-03-01' AND '2014-03-31' ) FROM RT_CUSTALLO ...
- Linux之select系统调用_2
在上一篇博文中,我们的程序中我们有3个客户端,因此也事先建立了3个管道,每个客户端分别使用一个管道向服务器发送消息.而在服务器端使用select系统调用,只要监测到某一管道有消息写入,服务器就将其re ...
- JDBC第二次学习
脑子太笨,必须得记录下来一些文字,方便回来查询. 这是我的第二次学习JDBC的笔记,看的是传智播客——李勇老师的JDBC系列,已看到第23集. 分析在实际项目中该如何应用JDBC 一个简单用户相关的数 ...
- redis系列之redis是什么
一.简介 REmote DIctionary Server(Redis),redis是一个基于内存的单机key/value系统,类似memcached,但支持value为多种形式,包括:字符串(str ...
- (.iso)光盘镜像文件的打开与安装
直接解压就可以打开,然后就可以安装.exe文件
- JLINK固件丢失或升级固件后提示Clone的解决办法
J-LINK V8固件烧录指导 J-LINK 是使用过程中,如果内部固件意外损坏或丢失,请参考下面操作步骤说明,重新烧录JLINK固件. 安装固件烧录软件 请ATMEL官方网址下载AT91-ISP下载 ...
- Qt 显示图片 放大 缩小 移动(都是QT直接提供的功能)
本文章原创于www.yafeilinux.com 转载请注明出处. 现在我们来实现在窗口上显示图片,并学习怎样将图片进行平移,缩放,旋转和扭曲.这里我们是利用QPixmap类来实现图片显示的. 一.利 ...
- 几个Unicode新知识:扩展ANSI有很多种(256个字符),Unicode表示ANSI字符时高字节为0,Unicode不包括古代字符
都是有些模糊的概念,特别是Unicode不包括古代字符让我有点惊讶.看来Unicode只适用于大多数情况,一旦有无法表示的字符,那该怎么办呢? ANSI针对英语设计的,当处理带有音调标号(形如汉语的拼 ...