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 ...
随机推荐
- SQL Server 2008连接字符串写法大全{转}
一..NET Framework Data Provider for SQL Server 类型:.NET Framework类库使用:System.Data.SqlClient.SqlConnect ...
- list<T> 自定义比较器进行排序
今天在研究List<T> 集合如何排序,我试过很多,但是都不行,然后看到msdn中的这个比较器排序,自己测试了代码,No Problem.给大家分享一下. 类型 T 的默认比较器按如下方式 ...
- Android中如何查看内存(上)
文章参照自:http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-a ...
- java基础知识回顾之javaIO类---FileInputStream和FileOutputStream字节流复制图片
package com.lp.ecjtu; import java.io.FileInputStream; import java.io.FileNotFoundException; import j ...
- Java集合框架(三)
Map Map集合:该集合存储键值对,一对一对的往里存,而且要保证键的唯一性. Map |------HashTable:底层是哈希表数据结构,不可以存入null键null值.该集合是线程同步的.J ...
- daatable动态创建
方法一: DataTable tblDatas = new DataTable("Datas");DataColumn dc = null;dc = tblDatas.Column ...
- MongoDB (四) MongoDB 数据模型
在 MongoDB 中的数据有灵活的模式.在相同集合中文档并不需要有相同的一组字段或结构的公共字段的集合,文档可容纳不同类型的数据. MongoDB设计模式的一些考虑 可根据用户要求设计架构. 合并对 ...
- YII框架的依赖注入容器与服务定位器简述
依赖注入容器 依赖注入(Dependency Injection,DI)容器就是一个对象use yii\di\Container,它知道怎样初始化并配置对象及其依赖的所有对象. 依赖注入和服务定位器都 ...
- python FTP上传和下载文件
1. 连接FTP server import ftplib ftp = ftplib.FTP(ftpserver, user, passwd) 等同于 import ftplib ftp = ftpl ...
- 【转】SIP初步
1.什么是SIP SIP(会话发起协议)属于IP应用层协议,用于在IP网上为用户提供会话应用.会话(Session)指两方或多方用户之间的语音.视频.及其他媒体形式的通信,具体可能是IP电话.会议.即 ...