题意:求一个字符串的不重叠最长相同变化的子串

n<=20000

思路:这是一道论文题

我们将原串两两之间作差,可以发现所求的相同变化的子串作出的差相同

问题就转化成了不重叠的最长重复子串

显然答案有二分性,二分答案,将问题转化为是否存在长度为k的相同子串

将后缀分成连续的若干组,每组相邻的height都不小于k

有希望满足条件的两个后缀一定在同一组

只要判断某组中的sa最大与最小值之差是否不小于k即可

有一组满足则k满足

 var a,b,wc,wd,x,y,height,sa,rank:array[..]of longint;
n,i,l,r,mid,last,m:longint; function cmp(a,b,l:longint):boolean;
begin
exit((y[a]=y[b])and(y[a+l]=y[b+l]));
end; procedure swap(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
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
begin
if cmp(sa[i-],sa[i],j) then x[sa[i]]:=p-
else begin x[sa[i]]:=p; inc(p); end;
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 min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; function max(x,y:longint):longint;
begin
if x>y then exit(x);
exit(y);
end; function isok(x:longint):boolean;
var l,r,i:longint;
begin
l:=sa[]; r:=sa[];
for i:= to n- do
if height[i]<x then
begin
l:=sa[i]; r:=sa[i];
end
else
begin
l:=min(l,sa[i]);
r:=max(r,sa[i]);
if r-l>=x then exit(true);
end;
exit(false);
end; begin
assign(input,'poj1743.in'); reset(input);
assign(output,'poj1743.out'); rewrite(output);
while not eof do
begin
fillchar(a,sizeof(a),);
fillchar(b,sizeof(b),);
fillchar(sa,sizeof(sa),);
fillchar(rank,sizeof(rank),);
fillchar(x,sizeof(x),);
fillchar(y,sizeof(y),);
fillchar(height,sizeof(height),);
fillchar(wc,sizeof(wc),);
fillchar(wd,sizeof(wd),);
readln(n);
if n= then break;
for i:= to n- do read(b[i]);
dec(n);
for i:= to n- do a[i]:=b[i+]-b[i]+;
a[n]:=; m:=;
getsa(n+);
getheight(n);
l:=; r:=(n-) div ; last:=;
while l<=r do
begin
mid:=(l+r)>>;
if isok(mid) then begin last:=mid; l:=mid+; end
else r:=mid-;
end;
if last< then writeln()
else writeln(last+);
// for i:= to n do write(sa[i]+,' ');
// writeln;
// for i:= to n do writeln(height[i]); end; close(input);
close(output);
end.

【POJ1743】Musical Theme(后缀数组,二分)的更多相关文章

  1. POJ1743 Musical Theme(后缀数组 二分)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 33462   Accepted: 11124 Description A m ...

  2. POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串

    题目链接:https://vjudge.net/problem/POJ-1743 Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Tot ...

  3. Poj 1743 Musical Theme(后缀数组+二分答案)

    Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...

  4. POJ 1743 [USACO5.1] Musical Theme (后缀数组+二分)

    洛谷P2743传送门 题目大意:给你一个序列,求其中最长的一对相似等长子串 一对合法的相似子串被定义为: 1.任意一个子串长度都大于等于5 2.不能有重叠部分 3.其中一个子串可以在全部+/-某个值后 ...

  5. POJ1743 Musical Theme [后缀数组]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  6. POJ1743 Musical Theme [后缀数组+分组/并查集]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  7. Poj 1743 Musical Theme (后缀数组+二分)

    题目链接: Poj  1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...

  8. POJ-1743 Musical Theme(后缀数组)

    题目大意:给一个整数序列,找出最长的连续变化相同的.至少出现两次并且不相重叠一个子序列. 题目分析:二分枚举长度进行判定. 代码如下: # include<iostream> # incl ...

  9. poj1743 Musical Theme 后缀数组的应用(求最长不重叠重复子串)

    题目链接:http://poj.org/problem?id=1743 题目理解起来比较有困难,其实就是求最长有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1 ...

  10. POJ 1743 Musical Theme ——后缀数组

    [题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include ...

随机推荐

  1. Haproxy+Rabbitmq中的问题

    问题一.Rabbitmq集群搭建完成 某个集群节宕机后 无法添加失败 解决办法:停掉所有Rabbitmq服务 并删除集群文件C\Users\Administrator\AppData\Roaming\ ...

  2. jquery实现上传图片及图片大小验证、图片预览效果代码

    jquery实现上传图片及图片大小验证.图片预览效果代码 jquery实现上传图片及图片大小验证.图片预览效果代码 上传图片验证 */ function submit_upload_picture() ...

  3. Apache的HttpClient的使用

    Apache的HttpClient可以被用于从客户端发送HTTP请求到服务器端,其中封装了客户端发送http的get和post请求 使用Apache的HttpClient发送GET和POST请求的步骤 ...

  4. js 字符串截取 substring() 方法、 substr() 方法、slice() 方法、split() 、join();

    三种 js 截取字符串的方法: substring() 方法: substr() 方法: slice() 方法: 1.:substring() 方法:string.substring(from, to ...

  5. Python3简明教程(七)—— 字符串

    字符串是 Python 中最常用的数据类型.本节实验将会学习如何对 Python3 的字符串进行处理操作. 字符串的三种表示 可以通过几种不同的方式表示字符串.如单引号('...')或双引号(&quo ...

  6. 网新恩普(T 面试)

    1.你们项目中哪里用到了Redis? 2.介绍一下你的这个微服务项目

  7. 最短路 || POJ 1797 Heavy Transportation

    Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...

  8. Mac 下用homebrew安装配置MongoDB

    ---恢复内容开始--- 1.首先安装homebrew,已有就跳过 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent. ...

  9. 使用 Pytorch 实现 skip-gram 的 word2vec

    转载请注明 AIQ - 最专业的机器学习大数据社区  http://www.6aiq.com AIQ 机器学习大数据 知乎专栏 点击关注 链接地址: https://github.com/lonePa ...

  10. 微信小程序工具真机调试提示page "xxx/xxx/xxx" is not found

    解决方法: pages对象添加该页面