【Ural1297】Palindrome(后缀数组)
题意:求一个字符串的最长回文子串
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(后缀数组)的更多相关文章
- 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) ...
- 1297. Palindrome ural1297(后缀数组)
1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...
- URAL 1297 Palindrome 后缀数组
D - Palindrome Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- URAL - 1297 Palindrome —— 后缀数组 最长回文子串
题目链接:https://vjudge.net/problem/URAL-1297 1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB ...
- URAL 1297 Palindrome (后缀数组+RMQ)
题意:给定一个字符串,求一个最长的回回文子串,多解输出第一个. 析:把字符串翻转然后放到后面去,中间用另一个字符隔开,然后枚举每一个回文串的的位置,对第 i 个位置,那么对应着第二个串的最长公共前缀, ...
- Ural1297 Palindrome(后缀数组)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12406 [题意] 求最长回文子串. [思路] 将字符串 ...
- 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 ...
- URAL 1297 Palindrome(后缀数组+ST表)
[题目链接] http://acm.timus.ru/problem.aspx?num=1297 [题目大意] 求最长回文子串,并输出这个串. [题解] 我们将原串倒置得到一个新的串,加一个拼接符将新 ...
- UVA 11475 Extend to Palindrome(后缀数组+ST表)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/27647 [题目大意] 给出一个字符串,要求在其后面添加最少的字符数,使得其成为一个回文串.并输出这个回文串 ...
- UVA - 11475 Extend to Palindrome —— 字符串哈希 or KMP or 后缀数组
题目链接:https://vjudge.net/problem/UVA-11475 题意: 给出一个字符串,问在该字符串后面至少添加几个字符,使得其成为回文串,并输出该回文串. 题解: 实际上是求该字 ...
随机推荐
- log4j 日志分级处理
log4j 配置文件: log4j.rootLogger=debug,stdout,debug,info,errorlog4j.appender.stdout=org.apache.log4j.Con ...
- 使用ABAP正则表达式解析HTML标签
需求就是我用ABAP的某个函数从数据库读取一个字符串出来,该字符串的内容是一个网页. 网页的form里包含了很多隐藏的input field.我的任务是解析出name为svyValueGuid的inp ...
- (转)编码剖析Spring装配基本属性的原理
http://blog.csdn.net/yerenyuan_pku/article/details/52856465 上回我们已经讲到了Spring依赖注入的第一种方式,现在我们来详解第二种方式,须 ...
- 查看本机的ip地址
ifconfig可以查看本机的ip地址:inet addr:10.108.104.185
- linux 安装nginx 集成emq
1:下载nginx-1.12.2.tar.gz http://nginx.org/en/download.html 2:解压 tar -zxvf nginx-1.12.2.tar.gz 3:进行co ...
- install mysql at linux
cd /usr/local wget http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm rpm -ivh mysql5 ...
- 洛谷 P1708 天然气井 题解
https://www.luogu.org/problemnew/show/P1708 这道题还是比较好的. 读完题目我们先想想如何计算某个天然气井($x_1,y_1$)和中转站($a_1,b_1$) ...
- python:端口扫描邮件推送
#!/usr/bin/env python import pickle import smtplib from email.mime.text import MIMEText import nmap ...
- spring-mvc jackson配置json为空不输出
使用的spring-mvc版本是4.1.6,jackson版本是2.1.4 在spring-mvc配置文件中添加以下代码就行 <mvc:annotation-driven> <mvc ...
- 南邮CTF--md5_碰撞
南邮CTF--难题笔记 题目:md5 collision (md5 碰撞) 解析: 经过阅读源码后,发现其代码是要求 a !=b 且 md5(a) == md5(b) 才会显示flag,利用PHP语言 ...