[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,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- DNS隧道之DNS2TCP实现——dns2tcpc必须带server IP才可以,此外ssh可以穿过墙的,设置代理上网
我自己的命令: server端: dns2tcpd -F -d 1 -f ./dns2tcpd.conf 输出: 09:08:59 : Debug options.c:97 Add resource ...
- C#读出文本文件内容,遍历数组筛选出 含有汉字对应的拼音字符
情景描述:由于任务需要,现有一用户表数据,用户名 字段 在新增用户时,输入中文和拼音两种,先要区分同时含有中文和拼音字母的用户名.由于数据很多,可以通过一段代码完成查询: 前提:在阅读本文之前可以先了 ...
- Docker运行程序报错 WARNING: IPv4 forwarding is disabled. Networking will not work
WARNING: IPv4 forwarding is disabled. Networking will not work. 第一步:vi /usr/lib/sysctl.d/00-system ...
- 在 Ubuntu 15.04 上安装 Android Studio(极其简单)
sudo apt-add-repository ppa:paolorotolo/android-studio sudo apt-get update sudo apt-get install andr ...
- CSS实现栅格布局
CSS实现栅格布局 设置容器container: .grid-container { width: 100%; max-width: 1200px; } 清除浮动: .row:before, .row ...
- 设置cookie,删除cookie,读取cookie
1.首先来说下cookie的作用 我们在浏览器中,经常涉及到数据的交换,比如你登录邮箱,登录一个页面.我们经常会在此时设置30天内记住我,或者自动登录选项.那么它们是怎么记录信息的呢,答案就是今天的主 ...
- js数组定义、属性及方法(push/pop/unshfit/shfit/reverse/sort/slice/splice/indexOf/lastIndexOf)
数组 一.定义数组 * 字面量方式 var 数组名称 = [ value,value,... ] * 构造函数方式 var 数组名称 = new Array(value,value,...): v ...
- IOS入门之创建视图和控件绑定
学习IOS几天了,跟着教程做了两个小应用了,现在先来总结一下.今天就是创建视图和绑带到代码了.其实就是常见的MVC模式实现. 使用的Xcode版本是8.2. 在Xcode创建项目之后,默认就会创建一个 ...
- C++ 简单内存泄漏检测方法
遇到个bug,MFC程序异常退出,debug模式下输出 Detected memory leaks! Dumping objects -> {366566} normal block at 0x ...
- MFC框架下Opengl窗口闪屏问题解决方案
转自https://blog.csdn.net/niusiqiang/article/details/43116153 虽然启用了双缓冲,但是仍然会出闪屏的情况,这是由于OpenGL自己有刷新背景的函 ...