[poj3974] Palindrome 解题报告 (hash\manacher)
题目链接:http://poj.org/problem?id=3974
题目:
多组询问,每组给出一个字符串,求该字符串最长回文串的长度
数据范围支持$O(nlog n)$
解法一:
二分+hash
回文串分奇数串和偶数串。对于奇数串,我们枚举它的中点,二分一下这个中点可以向两边扩展多远的距离;对于偶数串,我们枚举它中间两个点靠左的点,同样二分可以扩展的距离,这样时间复杂度就是$O(nlog n)$的了
说起来容易,写起来不是很容易
解法二:
每次跑一遍manacher就好了
说起来容易,写起来也很容易
解法一代码:
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdio>
typedef unsigned long long ull;
using std::string;
using std::cin;
using std::max;
using std::min; const int N=1e6+;
int cas,ans;
ull p[N],f[N],d[N];
string ch;
int main()
{
p[]=;
for (int i=;i<=N;i++) p[i]=p[i-]*;
while (cin>>ch)
{
if (ch=="END") break;
printf("Case %d: ",++cas);
memset(f,,sizeof(f));
memset(d,,sizeof(d));
int n=ch.size();
for (int i=;i<n;i++)
{
f[i+]=f[i]*+ch[i]-'a'+;
}
for (int i=n;i>=;i--)
{
d[i]=d[i+]*+ch[i-]-'a'+;
}
ans=;
for (int i=;i<=n;i++)
{
int l=,r=min(i,n-i+);
while (l<r)
{
int mid=l+r>>;
int L1=i-mid+,R1=i;
int L2=i,R2=i+mid-;
int tmp1=f[R1]-f[L1-]*p[R1-L1+],tmp2=d[L2]-d[R2+]*p[R2-L2+];
if (tmp1==tmp2) l=mid+;
else r=mid;
}
int L1=i-l+,R1=i;
int L2=i,R2=i+l-;
int tmp1=f[R1]-f[L1-]*p[R1-L1+],tmp2=d[L2]-d[R2+]*p[R2-L2+];
if (tmp1!=tmp2) l--;
ans=max(ans,*l-); l=,r=min(i,n-i);
while (l<r)
{
int mid=l+r>>;
int L1=i-mid+,R1=i;
int L2=i+,R2=i++mid-;
int tmp1=f[R1]-f[L1-]*p[R1-L1+],tmp2=d[L2]-d[R2+]*p[R2-L2+];
if (tmp1==tmp2) l=mid+;
else r=mid;
}
L1=i-l+,R1=i;
L2=i+,R2=i++l-;
tmp1=f[R1]-f[L1-]*p[R1-L1+],tmp2=d[L2]-d[R2+]*p[R2-L2+];
if (tmp1!=tmp2) l--;
ans=max(ans,*l);
}
printf("%d\n",ans);
}
return ;
}
解法二代码:
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<iostream> const int N=2e6+;
using std::string;
using std::cin;
using std::min;
using std::max;
string ch;
char s[N];
int hw[N];
int main()
{
int cas=;
while (cin>>ch)
{
if (ch=="END") return ;
printf("Case %d: ",++cas);
int n=ch.size();
memset(hw,,sizeof(hw));
s[]=s[]='#';
for (int i=;i<=n;i++)
{
s[i<<]=ch[i-];
s[i<<|]='#';
}
n=n*+;
s[n]=;
int mx=,mid;
for (int i=;i<n;i++)
{
if (i<mx) hw[i]=min(mid+hw[mid]-i,hw[(mid<<)-i]);
else hw[i]=;
for (;s[i+hw[i]]==s[i-hw[i]];hw[i]++);
if (hw[i]+i>mx)
{
mx=hw[i]+i;
mid=i;
}
}
int ans=;
for (int i=;i<n;i++) ans=max(ans,hw[i]);
printf("%d\n",ans-);
}
return ;
}
[poj3974] Palindrome 解题报告 (hash\manacher)的更多相关文章
- leetcode—Palindrome 解题报告
1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...
- 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...
- 【LeetCode】214. Shortest Palindrome 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀是否回文 判断前缀 相似题目 参考资料 日期 题 ...
- 【LeetCode】125. Valid Palindrome 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 列表生成式 正则表达式 双指针 日期 题目地址:https:/ ...
- LeetCode: Valid Palindrome 解题报告
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- [POJ3974]Palindrome(后缀数组 || manacher)
传送门 求一个串的最长回文子串的长度 1.后缀数组 把这个串反转后接到原串的后面,中间连一个没有出现过的字符. 然后求这个新字符串的某两个后缀的公共前缀的最大值即可. ——代码 #include &l ...
- [JSOI2008] [BZOJ1567] Blue Mary的战役地图 解题报告 (hash)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1567 Description Blue Mary最近迷上了玩Starcraft(星际争霸 ...
- POJ3974 Palindrome
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- BZOJ 2819 DFS序+线段树
非递归的DFS写炸了- 交了一个递归版的 过了---.. //By SiriusRen #include <cstdio> #include <cstring> #includ ...
- PHP配置优化:php-fpm配置解读
PHP-FPM是一个PHP FastCGI管理器,php-fpm.conf配置文件用于控制PHP-FPM管理进程的相关参数,比如工作子进程的数量.运行权限.监听端口.慢请求等等. 我们在编译安装PHP ...
- android 给imageView,文字等加上阴影[记录]
1.链接 https://github.com/Devlight/ShadowLayout 2.效果 3.code compile 'com.github.devlight.shadowlayout: ...
- Android蓝牙2.0连接以及数据接收发送
1.加入权限 <uses-feature android:name="android.hardware.bluetooth_le" android:required=&quo ...
- java 读写分离
源码地址:http://git.oschina.net/xiaochangwei 先回答下 1.为啥要读写分离? 大家都知道最初开始,一个项目对应一个数据库,基本是一对一的,但是由于后来用户及数据还有 ...
- XP访问WIN10共享打印机提示错误:操作无法完成,拒绝访问
XP系统添加打印机--连接到此计算机的本地打印机(取消自动检测)--创建新端口(LOCAL port)----输入端口名\\计算机名\打印机名.(例如:\\adubei\\HP lasjet 1020 ...
- POSIX Threads
POSIX Threads, usually referred to as pthreads, is an execution model that exists independently from ...
- Python3.7中的常用关键字
本文是在学习Python中遇到的一些关键字,作为日常总结的笔记. Python中有保留字/关键字 保留字就是在Python中预先保留的标识符,这些标识符在Python程序中具有特定用途,不能被程序员作 ...
- JS怎样写闰年
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- day07 分支,循环
目录 if(分支) if的语法 if...else... if...elif...else if的嵌套 for循环 for-else 语句 for循环的嵌套(重要) range介绍 while循环 w ...