【POJ 3974】 Palindrome
【题目链接】
http://poj.org/problem?id=3974
【算法】
解法1 :
字符串哈希
我们可以分别考虑奇回文子串和偶回文子串,从前往后扫描字符串,然后二分答案,检验可以用哈希
时间复杂度 : O(TNlog(N))
解法2
Manacher算法
这个算法可以在O(n)时间内求出最长回文子串,读者可以自行查阅资料,笔者不进行详细的介绍
两种方法的效率比对
显然,Manacher算法的复杂度是优于字符串哈希的,笔者的两份代码在POJ上的运行时间分别为4891MS和266MS
【代码】
代码1
/*
Algorithm : Hash
Time Complexity : O(Tnlog(n))
*/ #include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXL 1000010
typedef unsigned long long ULL;
const int P = ; int i,len,l,r,mid,tmp,ans,TC;
ULL f[MAXL],ha[MAXL],hb[MAXL];
char s[MAXL]; inline ULL getha(int l,int r)
{
return ha[r] - ha[l-] * f[r-l+];
}
inline ULL gethb(int l,int r)
{
int tmp = l;
l = len - r + ;
r = len - tmp + ;
return hb[r] - hb[l-] * f[r-l+];
} int main()
{ f[] = ;
for (i = ; i < MAXL; i++) f[i] = f[i-] * P;
while (scanf("%s",s+))
{
len = strlen(s+);
if (s[] == 'E') break;
for (i = ; i <= len; i++) ha[i] = ha[i-] * P + (s[i] - 'a' + );
reverse(s+,s+len+);
for (i = ; i <= len; i++) hb[i] = hb[i-] * P + (s[i] - 'a' + );
ans = ;
for (i = ; i <= len; i++)
{
l = ; r = min(i-,len-i);
tmp = ;
while (l <= r)
{
mid = (l + r) >> ;
if (getha(i-mid,i) == gethb(i,i+mid))
{
tmp = mid;
l = mid + ;
} else r = mid - ;
}
ans = max(ans,*tmp+);
l = ; r = min(i,len-i);
tmp = ;
while (l <= r)
{
mid = (l + r) >> ;
if (getha(i-mid+,i) == gethb(i+,i+mid))
{
tmp = mid;
l = mid + ;
} else r = mid - ;
}
ans = max(ans,*tmp);
}
printf("Case %d: %d\n",++TC,ans);
} return ; }
代码2
/*
Algorithm : Manacher
Time Complexity : O(TN)
*/ #include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 1000010 int TC;
char s[MAXN<<]; inline void Manacher()
{
int i,len,mx = ,pos = ,ans = ;
static char tmp[MAXN<<];
static int p[MAXN<<];
len = strlen(s+);
for (i = ; i <= len; i++)
{
tmp[*i-] = '#';
tmp[*i] = s[i];
}
tmp[len = * len + ] = '#';
for (i = ; i <= len; i++)
{
if (mx > i) p[i] = min(p[*pos-i],mx-i);
else p[i] = ;
while (i - p[i] >= && i + p[i] <= len && tmp[i-p[i]] == tmp[i+p[i]]) p[i]++;
if (i + p[i] - > mx)
{
mx = i + p[i] - ;
pos = i;
}
}
for (i = ; i <= len; i++) ans = max(ans,p[i]-);
printf("Case %d: %d\n",++TC,ans);
} int main()
{ while (scanf("%s",s+) != EOF)
{
if (s[] == 'E') break;
Manacher();
} return ; }
【POJ 3974】 Palindrome的更多相关文章
- 【POJ 3974】Palindrome
http://poj.org/problem?id=3974 Manacher模板题.Menci的博客讲得很好 有一点:Menci的代码中的right我感觉是代表能延伸到的最右端点的右边的点,因为r( ...
- 【POJ 1159】Palindrome
[POJ 1159]Palindrome 近期各种题各种奇葩思路已经司空见惯了...又新出个滚动数组= = 该题另一点须要知道 最少须要补充的字母数 = 原序列S的长度 - S和S'的最长公共子串长度 ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
随机推荐
- Vue-prop
HTML 中的特性名是大小写不敏感的,所以浏览器会把所有大写字符解释为小写字符.这意味着当你使用 DOM 中的模板时,camelCase (驼峰命名法) 的 prop 名需要使用其等价的 kebab- ...
- Xcode编译ffmpeg(2)
iOS: FFmpeg编译和使用问题总结 折磨了我近一周多时间的FFmpeg库编译问题终于解决了,必须得把这一段时间来遇到过的坑全写出来.如果急着解决问题,编译最新版本的FFmpeg库请直接看第二部分 ...
- libevent reference Mannual I
FYI:http://www.wangafu.net/~nickm/libevent-book/ This lib is a integral of asynchronous IO. we shoul ...
- PHP 锁机制
应用环境 解决高并发,库存为负数的情况 阻塞模式 如果其他进程已经加锁文件,当前进程会一直等其他进程解锁文件后继续执行 flock($fp, LOCK_EX) // 文件锁 非阻塞模式 如果其他进程已 ...
- Django 命令行调用模版渲染
首先我们需要进入 Django 的 shell python manage.py shell 渲染模版中的 name from django.template import Context,Templ ...
- 洛谷 1182 数列分段Section II
[题解] 最大值最小化,那么一般要联想到二分.二分一个最大值,然后check一下能否分成小于等于m段即可. #include<cstdio> #include<algorithm&g ...
- unigui的ini文件读写【6】
procedure THeaderFooterForm.writerParas; var IniFile : TIniFile; begin try IniFile:=TIniFile.Create( ...
- 【Codeforces 339C】Xenia and Weights
[链接] 我是链接,点我呀:) [题意] 在天平上放砝码 你要在左边放一下然后到右边放一下 一直重复这样放m次 每次你放在其中一边都要让另外一边的重量比你少 你可以用1~10中的某些砝码 问你要怎样放 ...
- Spring MVC学习总结(13)——Spring MVC集成Swagger时文档无法排序问题
添加排序属性: window.swaggerUi = new SwaggerUi({ ... apisSorter: "alpha", // can also ...
- 小a和uim之大逃离(洛谷 1373)
题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北部天边急涌过来,还伴着一道道闪电,一阵阵雷声.刹那间,狂风大作,乌云布满了天空,紧接着豆大的雨点从天空中打落下来,只见前方出现了一个 ...