【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 ...
随机推荐
- vi 命令学习(二)
[选中文本(可视模式)] v 可视模式 从光标位置开始按正常模式选择文本 V 可视行模式 选中光标经过的完整行 ctrl + v 可视块模式 垂直方向选中文本 [ 撤销和恢复撤销] u undo 撤销 ...
- Java基础(九)--反射
什么是反射? 在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性 这种动态获取的信息以及动态调用对象的方法的功能称为反射机制. 反射的前 ...
- 02C++基本语法
基本语法 2.1.1单行注释 // 2.1.2多行注释 /* * */ 2.1.3标识符 C++ 标识符是用来标识变量.函数.类.模块,或任何其他用户自定义项目的名称.一个标识符以字母 A-Z 或 a ...
- Java基本输入输出
Java基本输入输出 基本输入 基本输出 package com.ahabest.demo; public class Test { public static void main(String[] ...
- The following packages have unmet dependencies:
root@ubuntu:~# apt-get install open-iscsiReading package lists... DoneBuilding dependency treeReadin ...
- 洛谷——P1413 坚果保龄球
P1413 坚果保龄球 题目描述 PVZ这款游戏中,有一种坚果保龄球.zombie从地图右侧不断出现,向左走,玩家需要从左侧滚动坚果来碾死他们. 我们可以认为地图是一个行数为6,列数为60的棋盘.zo ...
- FileOutputStream将从一个文件中读取的内容写到另一个文件中
package com.janson.day2018082 import java.io.FileInputStream; import java.io.FileNotFoundException; ...
- 爬虫之Requests库
官方文档:http://cn.python-requests.org/zh_CN/latest/ 一.引子 import requests resp = requests.get("http ...
- LINUX-文件系统分析
badblocks -v /dev/hda1 检查磁盘hda1上的坏磁块 fsck /dev/hda1 修复/检查hda1磁盘上linux文件系统的完整性 fsck.ext2 /dev/hda1 修 ...
- LES on MCT