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

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. Springboot 1.X 在Weblogic 中的发布

    springboot在tomcat中的兼容性很好,但是如果要把Springboot项目发布在weblogic,尤其是老版本的Weblogic就会出现各种问题.经过本人的不懈努力及查询资料,终于将Spr ...

  2. [转]Intellij Idea自动添加注释的方法

    Intellij Idea自动添加注释的方法 阿历Ali 关注 2017.08.20 21:22* 字数 914 阅读 2741评论 0喜欢 6 程序媛阿历终于要写第一篇关于开发的文章了!!! 阿历用 ...

  3. PE刷题记

    PE 中文翻译 最喜欢做这种很有意思的数学题了虽然数学很垃圾 但是这个网站的提交方式好鬼畜啊qwq 1.Multiples of 3 and 5 直接枚举 2.Even Fibonacci numbe ...

  4. 新建cordova应用,插件开发教程系列(总目录)

    以下几篇是连续的教程,代码也是连续的,包括如下章节: 新建cordova应用 https://www.cnblogs.com/cannel/p/11074359.html 使用cordova把h5应用 ...

  5. Linux系统下查找文件的方法

    Linux系统下查找文件的方法 作者:Vashon 时间:20150419 方法一.在当前目录里查找所有名为以 java 开头的文件: find ./ -name "java*" ...

  6. Linux PHP的运行模式

    关系图 首先聊一下服务器,常见的web server类型有apache和nginx Apache工作模式 Apache的工作模式是Apache服务器在系统启动后,预先生成多个进程副本驻留在内存中,一旦 ...

  7. 使用JavaScript给对象修改注册监听器

    我们在开发一些大型前端项目时,会遇到这样一种情况,某个变量上有个字段.我们想知道是哪一段程序修改了这个变量上的字段.比如全局变量window上我们自定义了一个新字段_name,我们想知道到底有哪些程序 ...

  8. Dockerfile 中的 CMD和ENTRYPOINT 两兄弟

    CMD 先说老大 CMD 当一个容器准备好运行之后,需要找一个指定命令来创建一个初始进程并运行. 一,/bin/sh -c 因为某种意义上一个Dockerfile其实可以理解是一个简化版bash 脚本 ...

  9. Django-常用设置(settings.py)

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 当前项目的根目录,Django会依此来定位工程内的相关文件 ...

  10. OSI七层模型和TCP/IP五层模型详解

    OSI是一个开放性的通信系统互连参考模型,他是一个定义得非常好的协议规范.OSI模型有7层结构,每层都可以有几个子层. OSI的7层从上到下分别是 7 应用层 6 表示层 5 会话层 4 传输层 3 ...