[poj1743]Musical Theme后缀数组

题意:不可重叠最长重复子串

有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题。“主题”是整个音符序列的一个子串,它需要满足如下条件:

1.长度至少为5个音符。

2.在乐曲中重复出现。(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值)

3.重复出现的同一主题不能有公共部分。

二分答案,转化为存在性判定,后缀数组问题的套路解法

 1 #include <cstdlib>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include<iostream>
 6 #include<cstdlib>
 7 #define inf 0x3f3f3f3f
 8 using namespace std;
 9 const int N=200005;
10 int wa[N],wb[N],wv[N],wc[N];
11
12 bool cmp(int *r,int a,int b,int l){
13     return r[a]==r[b]&&r[a+l]==r[b+l];
14 }
15
16 void make_sa(int *r,int *sa,int n,int m){//m是至多有多少个字符,up的意思
17     int i,j,p,*x=wa,*y=wb;
18     for(i=0;i<m;i++) wc[i]=0;
19     for(i=0;i<n;i++) wc[x[i]=r[i]]++;
20     for(i=1;i<m;i++) wc[i]+=wc[i-1];
21     for(i=n-1;i>=0;i--) sa[--wc[x[i]]]=i;
22     for(j=1,p=1;p<n;j*=2,m=p){
23         for(p=0,i=n-j;i<n;i++) y[p++]=i;
24         for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
25         for(i=0;i<n;i++) wv[i]=x[y[i]];
26         for(i=0;i<m;i++) wc[i]=0;
27         for(i=0;i<n;i++) wc[wv[i]]++;
28         for(i=1;i<m;i++) wc[i]+=wc[i-1];
29         for(i=n-1;i>=0;i--) sa[--wc[wv[i]]]=y[i];
30         for(swap(x,y),p=1,x[sa[0]]=0,i=1;i<n;i++)
31             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
32     }
33     return;
34 }
35
36 int rank1[N],height[N],sa[N];//输出都是从1开始存的,rank从0开始,最后需要+1,读入从下标0
37 void make_height(int *r,int *sa,int n){
38     int i,j,k=0;
39     for(i=1;i<=n;i++) rank1[sa[i]]=i;
40     for(i=0;i<n;height[rank1[i++]]=k)
41         for(k?k--:0,j=sa[rank1[i]-1];r[i+k]==r[j+k];k++);
42     return;
43 }
44
45 int r[N],a[N];
46 int n,k;
47 bool check(int k){
48     int maxn,minn;
49     maxn=-inf,minn=inf;
50     for(int i=2;i<=n;i++){
51         if(height[i]>=k){
52             minn=min(min(sa[i],sa[i-1]),minn);//lcp一定在height中
53             maxn=max(max(sa[i],sa[i-1]),maxn);
54             if(maxn-minn>k) return 1;//这是距离
55         }else        maxn=-inf,minn=inf;
56
57     }
58     return 0;
59 }
60 int erfen(int l,int r){
61     while(l<r){
62         int mid=(l+r+1)>>1;
63         if(check(mid)) l=mid;
64         else r=mid-1;
65     }
66     l++;
67     if(l<5) l=0;
68     return l;
69 }
70
71 int main(){
72     ios::sync_with_stdio(0);
73     cin.tie(0);
74     cout.tie(0);
75     while(cin>>n&&n){
76         for(int i=0;i<n;i++) cin>>a[i];
77         for(int i=0;i<n-1;i++) r[i]=a[i+1]-a[i]+100;
78         r[--n]=0;
79         make_sa(r, sa, n+1,220);
80         make_height(r, sa, n);
81         int ans=erfen(0,n);
82         cout<<ans<<"\n";
83     }
84     return 0;
85 }
时间: 2024-10-14 21:24:50

[poj1743]Musical Theme后缀数组的相关文章

[POJ1743] Musical Theme (后缀数组)

题目概述: A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range 1..88, each representing a key on the piano. It is unfortunate but true that this representation of melodies ignores the notion of musical tim

poj 1743 Musical Theme(后缀数组)

Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 30544   Accepted: 10208 Description A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range 1..88, each representing a key on the

POJ 1743 Musical Theme 后缀数组 最长重复不相交子串

Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range 1..88, each representing a key on the piano. It

POJ - 1743 Musical Theme (后缀数组求不可重叠最长重复子串)

Description A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range 1..88, each representing a key on the piano. It is unfortunate but true that this representation of melodies ignores the notion of music

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

题目链接: Poj  1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现(或者经过调转出现,调转是主题同时加上或者减去同一个整数) 3:重复主题不能重叠 解题思路: 求调转重复出现的子串,那么主题之间的差值一定是不变的.可以求文本串s中相邻两个数的差值,重新组成一个新的文本串S,然后找S后缀串中最长公共不重叠前缀.rank相邻的后缀串,公共前缀一定最长,但是有可能重叠.

poj 1743 Musical Theme 后缀数组

题目链接 做出公差后找出最长不重叠子序列的长度. 后缀数组的模板, 二分长度k然后将height数组分组, 判断每一组内sa的最大值-sa的最小值是否大于等于k, 如果大于等于k则满足. 1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath>

POJ 1743 Musical Theme ——后缀数组

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

POJ 1743 Musical Theme 后缀数组 不可重叠最长重复子串

二分长度k 长度大于等于k的分成一组 每组sa最大的和最小的距离大于k 说明可行 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 20010; int s[maxn]; int sa[maxn]; int t[maxn], t2[maxn], c[maxn]; int rank[maxn], height[maxn]; void

POJ-1743 Musical Theme(最长不可重叠子串,后缀数组+二分)

A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range 1..88, each representing a key on the piano. It is unfortunate but true that this representation of melodies ignores the notion of musical timing; b