URAL1297:Palindrome——题解
http://acm.timus.ru/problem.aspx?space=1&num=1297
https://vjudge.net/problem/URAL-1297
给定一个字符串,求最长回文子串。
论文题,摘一下论文的图片,保证一下就看懂了。
(由于我摘不下来图片所以用了https://www.cnblogs.com/lidaxin/p/5002878.html的图片,代码也是参考的他)

emm要是还没看懂的话稍微解释一下吧。
也就是说,实际上我们是把反串接到正串后面,然后后缀数组高度数组处理LSP(最大公共前缀),这样的公共前缀实际上就是回文串的一半。
当然是有上述的两种情况的,就是奇数长和偶数长的回文串,当然很好处理。
最后为了O(n),我们RMQ预处理一下即可。
#include<cstdio>
#include<cmath>
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
const int N=;
char s[N],ans[N];
int n,m,sa[N],rk[N],height[N],w[N],f[N][],lg[N];
inline int qpow(int a){return <<a;}
inline bool pan(int *x,int i,int j,int k){
int ti=i+k<n?x[i+k]:-;
int tj=j+k<n?x[j+k]:-;
return x[i]==x[j]&&ti==tj;
}
inline void SA_init(){
int *x=rk,*y=height,r=;
for(int i=;i<r;i++)w[i]=;
for(int i=;i<n;i++)w[s[i]]++;
for(int i=;i<r;i++)w[i]+=w[i-];
for(int i=n-;i>=;i--)sa[--w[s[i]]]=i;
r=;x[sa[]]=;
for(int i=;i<n;i++)
x[sa[i]]=s[sa[i]]==s[sa[i-]]?r-:r++;
for(int k=;r<n;k<<=){
int yn=;
for(int i=n-k;i<n;i++)y[yn++]=i;
for(int i=;i<n;i++)
if(sa[i]>=k)y[yn++]=sa[i]-k;
for(int i=;i<r;i++)w[i]=;
for(int i=;i<n;i++)++w[x[y[i]]];
for(int i=;i<r;i++)w[i]+=w[i-];
for(int i=n-;i>=;i--)sa[--w[x[y[i]]]]=y[i];
swap(x,y);r=;x[sa[]]=;
for(int i=;i<n;i++)
x[sa[i]]=pan(y,sa[i],sa[i-],k)?r-:r++;
}
}
inline void height_init(){
int i,j,k=;
for(i=;i<=n;i++)rk[sa[i]]=i;
for(i=;i<n;i++){
if(k)k--;
else k=;
j=sa[rk[i]-];
while(s[i+k]==s[j+k])k++;
height[rk[i]]=k;
}
}
void st_init(){
for(int i=;i<=n;i++){
f[i-][]=height[i];
lg[i]=lg[i-];
if((<<lg[i]+)==i)lg[i]++;
}
for(int j=;j<=lg[n];j++){
for(int i=;i<n;i++){
if(i+qpow(j)->=n)break;
f[i][j]=min(f[i][j-],f[i+qpow(j-)][j-]);
}
}
}
int lcp(int a,int b){
int l=rk[a],r=rk[b];
if(r<l)swap(l,r);
l--;r--;
if(r<)return ;
l++;
int len=r-l+;
int k=lg[len];
int h=qpow(k);
return min(f[l][k],f[r-h+][k]);
}
int main(){
cin>>s;
m=strlen(s),n=m*+;
for(int i=;i<m;i++)ans[i]=s[i];
s[m]=;
for(int i=m+;i<n;i++){
s[i]=s[n-i-];
}
s[n++]=;
SA_init();
n--;
height_init();
st_init();
int maxn=,l,tmp;
for(int i=;i<n;i++){
tmp=lcp(i,n-i-);
if(*tmp->maxn){
maxn=*tmp-;
l=i-tmp+;
}
tmp=lcp(i,n-i);
if(*tmp>maxn){
maxn=*tmp;
l=i-tmp;
}
}
ans[l+maxn]='\0';
printf("%s\n",ans+l);
return ;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+
+++++++++++++++++++++++++++++++++++++++++++
URAL1297:Palindrome——题解的更多相关文章
- ural1297. Palindrome
1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...
- Ural1297 Palindrome(后缀数组)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12406 [题意] 求最长回文子串. [思路] 将字符串 ...
- POJ3974:Palindrome——题解
http://poj.org/problem?id=3974 题目大意: 求最大回文子串长度. ———————————————————— 马拉车板子题. 马拉车大概讲解: 首先在每两个字母之间插入‘# ...
- Valid Palindrome leetcode java
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- 【leetcode刷题笔记】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Leetcode 9. Palindrome Number(水)
9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...
- 2013-2014集训之DP
第一周: 经过漫长的时间,终于有时间来写一下结题报告. 地址http://acm.hust.edu.cn/vjudge/contest/view.action?cid=36180#overview A ...
- A. Karen and Morning
A. Karen and Morning time limit per test 2 seconds memory limit per test 512 megabytes input standa ...
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
本文出自 http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner 打开 这个专题一共有25题,刷完 ...
随机推荐
- python 函数定义顺序
#!/usr/bin/python # Hello World def order(): print("haha") print('Hello World!') order()
- 如何设置虚拟化的centos内、外网络通畅
首先要去确定你的本机(本地物理机)是通过以太网(插网线)上网的,还是通过wifi上网的.这个很重要. 如果是通过以太网去上网,那么虚拟化出来的系统,网络配置应当选择桥接模式. 当然了,也不一定非要用桥 ...
- TPO-10 C2 Return a literature book
TPO-10 C2 Return a literature book 第 1 段 1.Listen to a conversation between a student and an employe ...
- Unity编辑器 - 资源修改立即写入磁盘AssetDataBase.SaveAssets()
Unity编辑器 - 资源修改立即写入磁盘AssetDataBase.SaveAssets() 在编写编辑器时,如果需要修改Unity序列化资源(如Prefab,美术资源,ScriptableObje ...
- Spring Boot下的lombok安装 (日志) 不能识别log变量问题
参考地址:http://blog.csdn.net/blueheart20/article/details/52909775 ps:除了要加载依赖之外 还要安装lombok插件
- token接口的测法
接口一般都有权限的校验,一般是需要登录后才可以调用 对于接口的认证,一般通过两种方式来实现1.校验用户请求中是否包含某项指定的cookie2.校验用户的请求的header中是否包含某项指定的字段(to ...
- java核心技术 笔记
一 . 总览 1. 类加载机制:jdk内嵌的class_loader有哪些,类加载过程.--后面需要补充 2. 垃圾收集基本原理,常见的垃圾收集器,各自适用的场景.--后面需要补充 3. 运行时动态编 ...
- mysql 主从配置笔记
1.master配置 server-id=1 log-bin=mysql-bin binlog-do-db=testdata binlog-ignore-db=mysql 2.master增加用户 g ...
- Faster RCNN论文解析
Faster R-CNN由一个推荐区域的全卷积网络和Fast R-CNN组成, Fast R-CNN使用推荐区域.整个网络的结构如下: 1.1 区域推荐网络 输入是一张图片(任意大小), 输出是目标推 ...
- 将SqlDataReader 数据集转化为datatbale ,在将datatable 转化为iList
public IList GetModelList(string tablename, string where) { IList list = null; DataTable dataTable = ...