http://poj.org/problem?id=1743

这题是一道后缀数组的经典例题:求不可重叠最长重复子串。

题意:

有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题。“主题”是整个音符序列的一个子串,它需要满足如下条件:
1.长度至少为5个音符。
2.在乐曲中重复出现。(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值)
3.重复出现的同一主题不能有公共部分。

题解:

因为可能同时加上或减去一个数,所以首先要作差比较。将后一个数减去前一个数得到新的数列c。

注意:例如1 2 3 4 5 2 3 4 5 6 变为 1 1 1 1 1 -3 1 1 1 1 ,重复子串的第一位是会变化的,最后要+1。

二分重复子串的长度k,然后判断它是否可行。

截自论文的图:

注意(我打的时候犯的错误):

1.分组的时候忽略了最后一组的判断。

2.因为判断的重复子串长度比真实长度小1,所以要判断位置x-y>k而不是>=k

对于这个问题的一组debug数据:1 2 3 4 5 1 2 3 4 5 1 ans=5

3.rk[i]不能等于0,至少>=1,因为我的模板没有第二关键字的那些的rk补成0了

4.看到评论里很多人犯了的错误:k最少是5,否则输出0

贴几组数据(debug好帮手):

input:

12
1 2 3 4 5 6 7 8 9 10 11 12
0

11
1 1 1 1 1 1 1 1 1 1 1

9
1 1 1 1 1 1 1 1 1

11
1 2 3 5 4 1 2 3 5 4 1

33
3 2 1 1 2 1 2 3 2 2 3 3 2 2 1 2 1 2 2 2 1 3 1 1 1 2 2 3 1 2 1 1 2

1
100

300
48 15 74 57 17 52 51 20 86 85 24 19 23 34 81 54 12 3 86 41 45 64 23 32 18 17 68 43 83 86

61 22 48 47 50 21 1 12 19 16 78 21 64 27 71 50 65 42 68 11 30 25 45 72 23 44 10 81 36 39

19 46 45 34 8 87 42 45 1 12 67 28 62 13 88 19 71 42 65 42 60 83 86 49 45 72 31 56 10 9

12 35 75 70 45 14 64 55 50 17 17 20 51 28 70 44 45 27 16 26 87 49 58 40 57 35 12 18 35

57 30 56 29 31 40 34 23 53 34 16 73 27 84 54 75 57 6 20 13 35 8 18 63 17 66 52 41 15 20

54 3 57 22 20 85 19 24 54 63 41 82 32 57 43 76 22 11 21 54 16 61 27 16 42 39 25 2 44 1

39 12 34 83 45 14 28 61 19 72 42 79 49 34 56 41 35 36 14 11 17 78 28 44 27 26 49 40 35

18 57 56 31 34 53 16 27 54 57 20 35 18 17 52 15 31 14 13 36 27 22 5 44 43 18 21 40 3 14

41 44 7 22 5 4 39 2 41 44 7 6 41 28 19 30 9 8 3 14 29 12 31 26 21 32 15 6 29 36 43 22 1

4 15 38 13 8 7 22 29 4 19 26 41 28 19 18 21 4 27 22 25 20 39 18 17 28 7 6 37 4

22
1 2 3 2 1 2 3 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1

30
25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18
82 78 74 70 66 67 64 60 65 80
0

output:

6
5
0
5
5
0
22
7
5

代码:

 //poj1743
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std; const int N=,INF=(int)1e9;
int n,a[N],c[N],sa[N],rk[N],Rs[N],y[N],wr[N],h[N]; int minn(int x,int y){return x<y ? x:y;}
int maxx(int x,int y){return x>y ? x:y;} void get_sa(int cl,int m)
{
for(int i=;i<=cl;i++) rk[i]=c[i];//c[i]必须>=1
for(int i=;i<=m;i++) Rs[i]=;
for(int i=;i<=cl;i++) Rs[rk[i]]++;
for(int i=;i<=m;i++) Rs[i]+=Rs[i-];
for(int i=cl;i>=;i--) sa[Rs[rk[i]]--]=i; int ln=,p=;
while(p<cl)
{
int k=;
for(int i=cl-ln+;i<=cl;i++) y[++k]=i;
for(int i=;i<=cl;i++)
if(sa[i]>ln) y[++k]=sa[i]-ln;
for(int i=;i<=cl;i++)
wr[i]=rk[y[i]]; for(int i=;i<=m;i++) Rs[i]=;
for(int i=;i<=cl;i++) Rs[wr[i]]++;
for(int i=;i<=m;i++) Rs[i]+=Rs[i-];
for(int i=cl;i>=;i--) sa[Rs[wr[i]]--]=y[i]; for(int i=;i<=cl;i++) wr[i]=rk[i];
for(int i=cl+;i<=cl+ln;i++) wr[i]=;//debug:rk[i]不能等于0的原因:这里给没有第二关键字的补0了。
p=;rk[sa[]]=;
for(int i=;i<=cl;i++)
{
if(wr[sa[i]]!=wr[sa[i-]] || wr[sa[i]+ln]!=wr[sa[i-]+ln]) p++;//debug '!='
rk[sa[i]]=p;
}
ln*=;m=p;//debug
}
sa[]=;rk[]=;
} void get_height(int cl)
{
int k=;
for(int i=;i<=cl;i++) if(rk[i]!=)
{
int j=sa[rk[i]-];
if(k) k--;
while(c[i+k]==c[j+k] && i+k<=cl && j+k<=cl) k++;
h[rk[i]]=k;
}
h[]=;
} bool check(int cl,int k)
{
int fir=,mn=INF,mx=;
for(int i=;i<=cl;i++)
{
if(h[i]<k)
{
if(mx-mn>k) return ;//debug:>=改成>才能保证差值不重复的情况下音符也不重复(第一个音符没有算进去)
fir=i;mn=INF;mx=;
}
else
{
mn=minn(mn,sa[i-]);
mx=maxx(mx,sa[i-]); }
mn=minn(mn,sa[i]);
mx=maxx(mx,sa[i]);
}
if(mx-mn>k) return ;//debug:最后一组不可忽略。
return ;
} int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
while()
{
scanf("%d",&n);
if(!n) return ;
int mx=-,pre=,mn=INF;
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
c[i]=x-pre;
pre=x;
mx=maxx(mx,c[i]);
mn=minn(mn,c[i]);
}
for(int i=;i<=n;i++) c[i]=c[i]-mn+;//debug 因为rk[i]不能=0,c[i]也不能=0
mx=mx-mn+;
get_sa(n,mx);
get_height(n);
int l=,r=n;
while(l<r)
{
int mid=(l+r+)/;
if(check(n,mid-)) l=mid;
else r=mid-;
}
if(l>=) printf("%d\n",l);
else printf("0\n");
}
return ;
}

【poj1743-Musical Theme】不可重叠最长重复子串-后缀数组的更多相关文章

  1. poj 1743 Musical Theme(最长重复子串 后缀数组)

    poj 1743 Musical Theme(最长重复子串 后缀数组) 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复 ...

  2. POJ-1743 Musical Theme 字符串问题 不重叠最长重复子串

    题目链接:https://cn.vjudge.net/problem/POJ-1743 题意 给一串整数,问最长不可重叠最长重复子串有多长 注意这里匹配的意思是匹配串的所有元素可以减去或者加上某个值 ...

  3. POJ 1743 Musical Theme(不可重叠最长重复子串)

    题目链接:http://poj.org/problem?id=1743 题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一 ...

  4. poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14874   Accepted: 5118 De ...

  5. poj 2774 最长公共子串 后缀数组

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 25752   Accepted: 10 ...

  6. 【POJ1743】不可重叠最长重复子串

    题意:求一个字符串里两个不重叠的最长重复子串 代码如下: #include<cstdio> #include<cstdlib> #include<cstring> ...

  7. POJ 1743 (后缀数组+不重叠最长重复子串)

    题目链接: http://poj.org/problem?id=1743 题目大意:楼教主の男人八题orz.一篇钢琴谱,每个旋律的值都在1~88以内.琴谱的某段会变调,也就是说某段的数可以加减一个旋律 ...

  8. POJ 2774 Long Long Message [ 最长公共子串 后缀数组]

    题目:http://poj.org/problem?id=2774 Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total ...

  9. 【codevs3160】最长公共子串 后缀数组

    题目描述 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入 读入两个字符串 输出 输出最长公共子串的长度 样例输入 yeshowmuchiloveyoumydearmotherrea ...

随机推荐

  1. How to add a webpart to your website

          I have download a webpart that can play media on the website from the internet.Then how to add ...

  2. 29、phonegap入门

    0. PhoneGap介绍 0.1  什么是PhoneGap? PhoneGap是一个基于HTML.CSS.JS创建跨平台移动应程序的快速开发平台.与传统Web应用不同的是,它使开发者能够利用iPho ...

  3. 【BZOJ 1269】文本编辑器

    题目 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对"文本编辑器"做了一个抽象的定义: Move k:将 ...

  4. [B2B、B2C、C2C] 区别介绍

    最近在学习建站系统的时候,偶尔我们的老大会说几个自己所不太了解的名词“简称”,所以呢?我就总结了一下,如果有不全面的地方,还请博友们多多指点! B2B B2B(也有写成BTB)是指企业对企业之间的营销 ...

  5. 虚拟现实-VR-UE4-创建一个自定义的角色 Character

    我学习的资料使用的是老版本的ue4 新版本有好多都是不一样的,好多东西需要自己来摸索, 比如,在老板版本中,默认创建一个GameMode 是回自动创建构造函数发的,而新版本,是没有的,需要自己手动填写 ...

  6. Kotlin的Reified类型:怎样在函数内使用这一类型(KAD 14)

    作者:Antonio Leiva 时间:Mar 2, 2017 原文链接:https://antonioleiva.com/reified-types-kotlin/ 对于Java开发者来说,最懊恼的 ...

  7. golang交叉编译笔记

    GOOS:目标平台的操作系统(darwin.freebsd.linux.windows) GOARCH:目标平台的体系架构(386.amd64.arm) Mac 下编译 Linux 和 Windows ...

  8. [leetcode-652-Find Duplicate Subtrees]

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

  9. 制作一个简易计算器——基于Android Studio实现

    一个计算器Android程序的源码部分分为主干和细节两部分. 一.主干 1. 主干的构成 计算器的布局 事件(即计算器上的按钮.文本框)监听 实现计算 2. 详细解释 假设我们的项目名为Calcula ...

  10. QThread中的互斥、读写锁、信号量、条件变量

    该文出自:http://www.civilnet.cn/bbs/browse.php?topicno=78431 在gemfield的<从pthread到QThread>一文中我们了解了线 ...