[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,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- hdoj--1028--Ignatius and the Princess III(母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 数据格式与数据类型(Content-Type)
常见数据类型: 文本文件: 二进制文件: 编码后的图像文件: jpeg png 网络中爬取的数据: 1. Content-Type Media Types Type text text/css tex ...
- TLP电源管理
笔记本电脑电池坏了, 换了块电池, 顺手装了一下这个电源管理软件. https://linrunner.de/en/tlp/docs/tlp-linux-advanced-power-manage ...
- iframe刷新以及自适应高度
A页面中的iframe链接到B页面在B页面调用这个可以刷新父页面的iframe self.location.reload(); <iframe src="admin-list.htm ...
- PopupWindow实现点击外部不消失
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.alert_ip, null); final Po ...
- 蓝牙音箱BluetoothA2dp
package myapplication.com.mybuletooch; import android.support.v7.app.AppCompatActivity; import andro ...
- JAVA 日期工具类的总结
一般,在项目中,我们会会经常使用到日期的各种方式的处理,在各个业务逻辑操作中,都需要相关的日期操作,因此,实现项目中的日期工具类的提出,还是十分重要的,下面,就项目中常用到的日期的相关操作方式,做了一 ...
- hook的本质就是在本原可执行文件中加东西
hook的本质就是在本原可执行文件中加东西. 本质就是添加东西:
- 使用CablleStatement调用存储过程
/** * 使用CablleStatement调用存储过程 * @author APPle * */ public class Demo1 { /** * 调用带有输入参数的存储过程 * CALL p ...
- (fields.E304) Reverse accessor for 'UserProfile.groups' clashes with reverse accessor for 'User.groups'.
创建数据库models.py,在进行数据迁移时抛出一下异常: E:\Project\GuoJia>python manage.py makemigrations SystemCheckError ...