[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,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- oracle得到建表语句
第一种方法是使用工具,如:pl/sql developer,在[工具]--[导出用户对象]出现就可以得到建表脚本. 第二种方法是,sql语句. DBMS_METADATA.GET_DDL包可以得到数据 ...
- web.config配置文件使用总结
我们在开发web系统的时候,使用web.config进行配置是司空见惯的,那么web.confg到底是什么呢?什么时候使用web.config呢?有几种使用web.config方式呢? 如果不太明白的 ...
- Linux下编译安装Memcache
需要gcc,make,cmake,autoconf,libtool等工具,联网后,yum install -y gcc,make,cmake,autoconf,libtool 编译安装libevent ...
- JS 日历
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- 改变浏览器页面默认滚动条样式scrollbar
scrollbar-3d-light-color 设置或检索滚动条亮边框颜色scrollbar-highlight-color 设置或检索滚动条3D界面的亮边(ThreedHighlight)颜色sc ...
- POJ 2774 Long Long Message (后缀数组+二分)
题目大意:求两个字符串的最长公共子串长度 把两个串接在一起,中间放一个#,然后求出height 接下来还是老套路,二分出一个答案ans,然后去验证,如果有连续几个位置的h[i]>=ans,且存在 ...
- BZOJ 3524 [POI2014]KUR-Couriers (主席树)
题目大意:给你一个序列,求某个区间出现次数大于一半的数是什么 主席树裸题,刷刷水题提升自信= = #include <cstdio> #include <cstring> #i ...
- Linux版本分支图
- 2019-03-18 OpenCV Tesseract-OCR 下载 安装 配置(cv2 报错)
OpenCV 下载 安装 配置 1.下载和Python版本对应的版本,此为下载地址 2.安装(在powershell管理员模式下安装) pip3 install .\opencv_python-3.4 ...
- LCA题集
点的距离(模板题) 树中两点间的距离就是d[u] + d[v] - 2 * d[lca(u, v)] #include<bits/stdc++.h> #define REP(i, a, b ...