【POJ3693】Maximum repetition substring (SA)
这是一道神奇的题目..论文里面说得不清楚,其实是这样...如果一个长度为l的串重复多次,那么至少s[1],s[l+1],s[2*l+1],..之中有相邻2个相等...设这时为j=i*l+1,k=j+l,我们这时候借助SA和RMQ O(1)求出:m=lcp(j,k),这时候,重复次数至少ans=m div l+1 。 当然,我们枚举到不一定能够是最优啊,因为你枚举的不一定是字符串的首尾..那这时候怎么办?就是论文里面说的,向前和向后匹配。我们设t=l-m mod l..可以理解为,这时候 m mod l为多出来的字符,t就看成是前面少的字符个数..当然如果m mod l=0就不用管这个了...那也就是说,我们再判断是否lcp(j-t,k-t)>=l 如果成立,那么 ans++ ...因为可以多出一段..
上面就解决了求最长的问题,下面将关于字典序的这个...其实SA就是字典序了,只要枚举是否 lcp(sa[i],sa[i]+l)>=(ans-1)*l,若成立,显然当前sa[i]起始长度为l的字符串就是答案...
嗯..写完顿时觉得涨姿势了..
const maxn=;
var
rec,c,h,rank,sa,x,y:array[..maxn] of longint;
f:array[..maxn,..] of longint;
n,cas:longint;
s:ansistring; function max(x,y:longint):longint; begin if x>y then exit(x) else exit(y); end;
function min(x,y:longint):longint; begin if x<y then exit(x) else exit(y); end;
procedure swap(var x,y:longint); var tmp:longint; begin tmp:=x;x:=y;y:=tmp; end; procedure make;
var p,i,tot:longint;
begin
p:=;
while p<n do
begin
fillchar(c,sizeof(c),);
for i:= to n-p do y[i]:=rank[i+p];
for i:= n-p+ to n do y[i]:=;
for i:= to n do inc(c[y[i]]);
for i:= to n do inc(c[i],c[i-]);
for i:= to n do
begin
sa[c[y[i]]]:=i;
dec(c[y[i]]);
end;
fillchar(c,sizeof(c),);
for i:= to n do x[i]:=rank[i];
for i:= to n do inc(c[x[i]]);
for i:= to n do inc(c[i],c[i-]);
for i:= n downto do
begin
y[sa[i]]:=c[x[sa[i]]];
dec(c[x[sa[i]]]);
end;
for i:= to n do sa[y[i]]:=i;
tot:=;
rank[sa[]]:=;
for i:= to n do
begin
if (x[sa[i]]<>x[sa[i-]]) or (x[sa[i]+p]<>x[sa[i-]+p]) then inc(tot);
rank[sa[i]]:=tot;
end;
p:=p<<;
end;
for i:= to n do sa[rank[i]]:=i;
end; procedure makeh;
var i,j,p:longint;
begin
h[]:=; p:=;
for i:= to n do
begin
p:=max(p-,);
if rank[i]= then continue;
j:=sa[rank[i]-];
while (i+p<=n) and (j+p<=n) and (s[i+p]=s[j+p]) do inc(p);
h[rank[i]]:=p;
end;
end; procedure rmq;
var i,j:longint;
begin
for i:= to n do f[i,]:=h[i];
for i:= to trunc(ln(n)/ln()) do
for j:= to n-<<i+ do
f[j,i]:=min(f[j,i-],f[j+<<(i-),i-]);
end; procedure init;
var i,tot:longint;
begin
n:=length(s);
for i:= to n do x[i]:=ord(s[i]);
fillchar(c,sizeof(c),);
for i:= to n do inc(c[x[i]]);
for i:= to do inc(c[i],c[i-]);
for i:= to n do
begin
sa[c[x[i]]]:=i;
dec(c[x[i]]);
end;
rank[sa[]]:=;
tot:=;
for i:= to n do
begin
if x[sa[i]]<>x[sa[i-]] then inc(tot);
rank[sa[i]]:=tot;
end;
make;
makeh;
rmq;
end; function lcp(x,y:longint):longint;
var t:longint;
begin
x:=rank[x]; y:=rank[y];
if x>y then swap(x,y);
if x<y then inc(x);
t:=trunc(ln(y-x+)/ln());
exit(min(f[x,t],f[y-<<t+,t]));
end; procedure solve;
var m,l,i,j,tmp,t,ans,cnt:longint;
pd:boolean;
begin
init;
fillchar(rec,sizeof(rec),);
ans:=;
for l:= to n- do
begin
i:=;
while i+l<=n do
begin
m:=lcp(i,i+l);
tmp:=m div l+;
t:=l-m mod l;
t:=i-t;
if (t>) and (m mod l<>) and (lcp(t,t+l)>=m) then inc(tmp);
if tmp>ans then
begin
cnt:=;
rec[]:=l;
ans:=tmp;
end;
if cnt=ans then
begin
inc(cnt);
rec[cnt]:=l;
end;
i:=i+l;
end;
end;
pd:=false;
for i:= to n do
if not pd then
for j:= to cnt do
begin
l:=rec[j];
if lcp(sa[i],sa[i]+l)>=(ans-)*l then
begin
t:=sa[i];
l:=l*ans;
pd:=true;
break;
end;
end
else break;
inc(cas);
write('Case ',cas,': ');
for i:= t to t+l- do write(s[i]);
writeln;
end; Begin
cas:=;
readln(s);
while s[]<>'#' do
begin
solve;
readln(s);
end;
End.
【POJ3693】Maximum repetition substring (SA)的更多相关文章
- 【poj3693】Maximum repetition substring(后缀数组+RMQ)
题意:给定一个字符串,求重复次数最多的连续重复子串. 传说中的后缀数组神题,蒟蒻真的调了很久才对啊.感觉对后缀数组和RMQ的模版都不是很熟,导致还是会有很多各种各样的小错误= = 首先,枚举重复子串的 ...
- 【poj3693】 Maximum repetition substring
http://poj.org/problem?id=3693 (题目链接) 题意 给定一个字符串,求重复次数最多的连续重复子串,若存在多组解,输出字典序最小的. Solution 后缀数组论文题,就是 ...
- 【Poj-3693】Maximum repetition substring 后缀数组 连续重复子串
POJ - 3693 题意 SPOJ - REPEATS的进阶版,在这题的基础上输出字典序最小的重复字串. 思路 跟上题一样,先求出最长的重复次数,在求的过程中顺便纪录最多次数可能的长度. 因为sa数 ...
- 【SPOJ687&POJ3693】Maximum repetition substring(后缀数组)
题意: n<=1e5 思路: From http://hzwer.com/6152.html 往后匹配多远 r 用ST表求lcp即可...往前 l 就把串反过来再做一下.. 但是有可能求出来的最 ...
- 【po3693】Maximum repetition substring
题意: 给定一个字符串 求重复次数最多的连续重复子串 并输出字典序最小方案 题解: 枚举子串长度L 显然如果重复次数>1 那么答案串肯定包含s[1],s[1+L],s[1+L*2],...中的两 ...
- 【POJ 3693】Maximum repetition substring 重复次数最多的连续重复子串
后缀数组的论文里的例题,论文里的题解并没有看懂,,, 求一个重复次数最多的连续重复子串,又因为要找最靠前的,所以扫的时候记录最大的重复次数为$ans$,扫完后再后从头暴力扫到尾找重复次数为$ans$的 ...
- POJ-3693/HDU-2459 Maximum repetition substring 最多重复次数的子串(需要输出具体子串,按字典序)
http://acm.hdu.edu.cn/showproblem.php?pid=2459 之前hihocoder那题可以算出最多重复次数,但是没有输出子串.一开始以为只要基于那个,每次更新答案的时 ...
- POJ3693 Maximum repetition substring —— 后缀数组 重复次数最多的连续重复子串
题目链接:https://vjudge.net/problem/POJ-3693 Maximum repetition substring Time Limit: 1000MS Memory Li ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
随机推荐
- 手机上如何远程控制Linux服务器?
这里介绍3个手机软件,分别是JuiceSSH.Termius和Termux,这3个软件都可以实现远程控制Linux服务器(相当于手机SSH客户端),而且使用起来都非常方便,下面我简单介绍一下这3个软件 ...
- 2018.6.15 Java对象序列化详解
一.定义 Serializable 序列化:把Java对象转换为字节序列的过程. 反序列化:把字节序列恢复为Java对象的过程. ObjectOutputStream对象输出流 可以将实现了Seria ...
- 5.1 Object类型
创建Object实例的方式有两种 ① 使用new操作符跟Object构造函数 var person = new Object(); person.name = "Tom"; pei ...
- os.walk 模块
os.walk()可以得到一个三元tupple(dirpath, dirnames, filenames),其中第一个为起始路径,第二个为起始路径下的文件夹,第三个是起始路径下的文件. 其中dirpa ...
- Ribbon 负载均衡搭建
本机IP为 192.168.1.102 1. 新建Maven 项目 ribbon 2. pom.xml <project xmlns="http://maven.ap ...
- MSBuild常用方法
打包后把nuget包复制到指定的目录 <Target Name="CopyPackage" AfterTargets="Pack"> <Cop ...
- 神经网络系列学习笔记(二)——神经网络之DNN学习笔记
一.单层感知机(perceptron) 拥有输入层.输出层和一个隐含层.输入的特征向量通过隐含层变换到达输出层,在输出层得到分类结果: 缺点:无法模拟稍复杂一些的函数(例如简单的异或计算). 解决办法 ...
- JZOJ 3509. 【NOIP2013模拟11.5B组】倒霉的小C
3509. [NOIP2013模拟11.5B组]倒霉的小C(beats) (File IO): input:beats.in output:beats.out Time Limits: 1000 ms ...
- JZOJ 3383. 【NOIP2013模拟】太鼓达人
3383. [NOIP2013模拟]太鼓达人 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limits ...
- 42.VUE学习之--组件之子组件使用$on与$emit事件触发父组件实现购物车功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...