题意:求一个字符串的最长回文子串

n<=1000

思路:这是一道论文题

需要注意的细节:

1.奇偶分类

2.中间的分割符与最后的附加字母都是最小值,但两者不能相同,否则height可能会出现问题

答案即为min(height[rank[x]+1]...height[rank[y]])

 var f:array[..,..]of longint;
x,y,sa,rank,height,a,wc,wd:array[..]of longint;
ch:ansistring;
n,m,i,k,max,tmp,t,j:longint; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; procedure swap(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
end; function cmp(a,b,l:longint):boolean;
begin
exit((y[a]=y[b])and(y[a+l]=y[b+l]));
end; procedure getsa(n:longint);
var i,j,p:longint;
begin
for i:= to n- do
begin
x[i]:=a[i];
inc(wc[a[i]]);
end;
for i:= to m- do wc[i]:=wc[i-]+wc[i];
for i:=n- downto do
begin
dec(wc[x[i]]);
sa[wc[x[i]]]:=i;
end;
j:=; p:=;
while p<n do
begin
p:=;
for i:=n-j to n- do
begin
y[p]:=i; inc(p);
end;
for i:= to n- do
if sa[i]>=j then begin y[p]:=sa[i]-j; inc(p); end;
for i:= to n- do wd[i]:=x[y[i]];
for i:= to m- do wc[i]:=;
for i:= to n- do inc(wc[wd[i]]);
for i:= to m- do wc[i]:=wc[i-]+wc[i];
for i:=n- downto do
begin
dec(wc[wd[i]]);
sa[wc[wd[i]]]:=y[i];
end;
for i:= to n do swap(x[i],y[i]);
p:=; x[sa[]]:=;
for i:= to n- do
if cmp(sa[i-],sa[i],j) then x[sa[i]]:=p-
else begin x[sa[i]]:=p; inc(p); end;
j:=j*;
m:=p;
end;
end; procedure getheight(n:longint);
var i,j,k:longint;
begin
k:=;
for i:= to n do rank[sa[i]]:=i;
for i:= to n- do
begin
if k> then dec(k);
j:=sa[rank[i]-];
while a[i+k]=a[j+k] do inc(k);
height[rank[i]]:=k;
end;
end; function query(x,y:longint):longint;
var len,l:longint;
begin
len:=y-x+; l:=trunc(ln(len)/ln());
exit(min(f[x,l],f[y-(<<l)+,l]));
end; function lcp(x,y:longint):longint;
var i,j:longint;
begin
//inc(x); inc(y);
i:=rank[x]; j:=rank[y];
if i>j then swap(i,j);
inc(i);
exit(query(i,j));
end; procedure init;
begin
fillchar(a,sizeof(a),);
fillchar(height,sizeof(height),);
fillchar(sa,sizeof(sa),);
fillchar(rank,sizeof(rank),);
fillchar(f,sizeof(f),);
fillchar(x,sizeof(x),);
fillchar(y,sizeof(y),);
fillchar(wc,sizeof(wc),);
fillchar(wd,sizeof(wd),);
end; begin
assign(input,'ural1297.in'); reset(input);
assign(output,'ural1297.out'); rewrite(output);
while not eof do
begin
init;
readln(ch);
n:=length(ch);
if n= then break;
for i:= to n- do a[i]:=ord(ch[i+]);
a[n]:=; m:=;
for i:=n+ to *n do a[i]:=ord(ch[n-(i-n)+]);
a[n*+]:=;
getsa(n*+);
getheight(n*+);
m:=n*+;
t:=trunc(ln(m)/ln());
for i:= to m do f[i,]:=height[i];
for i:= to t do
for j:= to m do
if j+(<<(i-))<=m then f[j,i]:=min(f[j,i-],f[j+(<<(i-)),i-]);
max:=; k:=;
for i:= to n- do
begin
tmp:=lcp(i,*n-i)*-;
if tmp>max then
begin
max:=tmp;
k:=i;
end;
if i> then
begin
tmp:=lcp(i,n*-i+)*;
if tmp>max then
begin
max:=tmp;
k:=i;
end;
end;
end;
if max mod = then
for j:=k-max div + to k+max div + do write(ch[j])
else
for j:=k-max div + to k+max div do write(ch[j]);
writeln;
// for i:= to n*+ do writeln(height[i]);
// for i:= to n*+ do writeln(rank[i]);
// writeln;
end; close(input);
close(output);
end.

【Ural1297】Palindrome(后缀数组)的更多相关文章

  1. UVA - 11475 Extend to Palindrome (后缀数组)

    Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse it) ...

  2. 1297. Palindrome ural1297(后缀数组)

    1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  3. URAL 1297 Palindrome 后缀数组

    D - Palindrome Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  4. URAL - 1297 Palindrome —— 后缀数组 最长回文子串

    题目链接:https://vjudge.net/problem/URAL-1297 1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB ...

  5. URAL 1297 Palindrome (后缀数组+RMQ)

    题意:给定一个字符串,求一个最长的回回文子串,多解输出第一个. 析:把字符串翻转然后放到后面去,中间用另一个字符隔开,然后枚举每一个回文串的的位置,对第 i 个位置,那么对应着第二个串的最长公共前缀, ...

  6. Ural1297 Palindrome(后缀数组)

        [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12406 [题意] 求最长回文子串. [思路] 将字符串 ...

  7. Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  8. URAL 1297 Palindrome(后缀数组+ST表)

    [题目链接] http://acm.timus.ru/problem.aspx?num=1297 [题目大意] 求最长回文子串,并输出这个串. [题解] 我们将原串倒置得到一个新的串,加一个拼接符将新 ...

  9. UVA 11475 Extend to Palindrome(后缀数组+ST表)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/27647 [题目大意] 给出一个字符串,要求在其后面添加最少的字符数,使得其成为一个回文串.并输出这个回文串 ...

  10. UVA - 11475 Extend to Palindrome —— 字符串哈希 or KMP or 后缀数组

    题目链接:https://vjudge.net/problem/UVA-11475 题意: 给出一个字符串,问在该字符串后面至少添加几个字符,使得其成为回文串,并输出该回文串. 题解: 实际上是求该字 ...

随机推荐

  1. Git之提交项目到远程github

    1.在分支dev下,默认本地工作区有项目project 2. git add project  [添加项目到暂存区] 3. git commit project -m "提交项目" ...

  2. 序列化pickle模块

    1.pickle模块 pickle.dumps() 和pickle.loads() import pickle f = open('112.pkl','w') a = {'name':2,2:3,3: ...

  3. jsapi4加载的首个图层的范围被默认作为地图范围,且不能修改的解决

    在map加载的第一个图层的图层范围(fullExtent),会被默认设置为map的全图范围,且不能更改,从一般地图控件角度来说,应该有fullExtent属性,作为地图的全图范围,但很遗憾jsapi4 ...

  4. laravel模型关联

    hasOne 一对一 用户名-手机号hasMany 一对多   文章-评论belongTo 一对多反向 评论-文章belongsToMany    多对多 用户-角色hasManyThrough 远程 ...

  5. [Python3]Python官方文档-Python Manuals

    简介 一般情况下,初学者都不愿意直接去浏览Python Manuals,即Python自带的官方文档.尤其是只有英文版的情况下,初学者更加不会去使用该官方文档了. 在这里笔者强力推荐初学者经常学会使用 ...

  6. HYSBZ 1086 王室联邦 (树的分块)

    题意:国王想把他的国家划分成若干个省.他的国家有n个城市,是一棵树,即n-1条边,编号为1..n.为了防止管理太过分散,每个省至少要有B个城市,为了能有效的管理,每个省最多只有3B个城市.每个省必须有 ...

  7. 一个JSON字符串和文件处理的命令行神器jq,windows和linux都可用

    这个命令行神器的下载地址:https://stedolan.github.io/jq/# Windows和Linux版本均只有两个可执行文件,大小不过2MB多. 以Windows版本为例,介绍其用法. ...

  8. PHP一句话后门过狗姿势万千之理论篇

    写在前面: 过狗相关的资料网上也是有很多,所以在我接下来的文章中,可能观点或者举例可能会与网上部分雷同,或者表述不够全面. 但是我只能说,我所传达给大家的信息,是我目前所掌握或者了解的,不能保证所有人 ...

  9. uva12099 The Bookcase

    这道题超经典.dp和优化都值得看一看.因为i+1只和i有关,用滚动数组节省空间暑假第一次做感觉很困难,现在看就清晰了很多 #include<cstdio> #include<cstr ...

  10. PHP02 PHPStrom2018.X与WAMPServer3.0.6的集成

    脚本运行环境设置:设置PHPStorm中的脚本在PHP解析器上运行 1.进入Filie>>>setting>>languages and FrameWorks 选择php ...