POJ 1743后缀数组+二分

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; but, this programming task is about notes and not timings.
Many composers structure their music around a repeating &qout;theme&qout;, which, being a subsequence of an entire melody, is a sequence of integers in our representation. A subsequence of a melody is a theme if it:
  • is at least five notes long
  • appears (potentially transposed -- see below) again somewhere else in the piece of music
  • is disjoint from (i.e., non-overlapping with) at least one of its other appearance(s)

Transposed means that a constant positive or negative value is added to every note value in the theme subsequence.
Given a melody, compute the length (number of notes) of the longest theme.
One second time limit for this problem's solutions!
Input
The input contains several test cases. The first line of each test case contains the integer N. The following n integers represent the sequence of notes.
The last test case is followed by one zero.
Output
For each test case, the output file should contain a single line with a single integer that represents the length of the longest theme. If there are no themes, output 0.
Sample Input
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
Sample Output
5

有n个数,求不重叠的最长相同变化的子串,小于5的话就输出0。
比如1,2,3,4,5,6,7,8,9,10 1,2,3,4,5和6,7,8,9,10是都数变化为1且不重叠的两个,所以答案是5 。而对于1,2,3,4,5,6,7,8,9 最多只有1,2,3,4和5,6,7,8相同且不重叠所以答案是0。
可以用后缀数组中的 height数组可以求出来。
height[i]:表示前缀排名为i和前缀排名为i-1的最大公共前缀。
第56行必须是L+m+r <= R,只有这样成立才不会出现重叠。
 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdio.h>
 4 #define INF 0x3f3f3f3f
 5 using namespace std;
 6 const int MAX = 2e4+10;
 7 int *ran, r[MAX], sa[MAX], height[MAX], wa[MAX], wb[MAX], wm[MAX];
 8 bool cmp(int *r, int a, int b, int l) {
 9     return r[a]==r[b]&&r[a+l]==r[b+l];
10 }
11 void da(int *r, int *sa, int n, int m) {
12     int *x = wa, *y = wb, *t;
13     for(int i = 0; i < m; i ++) wm[i] = 0;
14     for(int i = 0; i < n; i ++) wm[x[i]=r[i]]++;
15     for(int i = 1; i < m; i ++) wm[i] += wm[i-1];
16     for(int i = n-1; i >= 0; i --) sa[--wm[x[i]]] = i;
17     for(int i = 0, j = 1, p = 0; p < n; j<<=1,m = p) {
18         for(p = 0, i = n-j; i < n; i ++) y[p++] = i;
19         for(int i = 0; i < n; i ++) if(sa[i] >= j) y[p++] = sa[i] - j;
20         for(int i = 0; i < m; i ++) wm[i] = 0;
21         for(int i = 0; i < n; i ++) wm[x[y[i]]]++;
22         for(int i = 0; i < m; i ++) wm[i] += wm[i-1];
23         for(int i = n-1; i >= 0; i --) sa[--wm[x[y[i]]]] = y[i];
24         for(t=x,x=y,y=t,i=p=1,x[sa[0]]=0; i < n; ++i) {
25             x[sa[i]] = cmp(y,sa[i],sa[i-1],j)?p-1:p++;
26         }
27     }
28     ran = x;
29 }
30 void calheight(int *r, int *sa, int n) {
31     for(int i=0,j=0,k=0;i < n; height[ran[i++]]=k){
32         for(k?--k:0,j=sa[ran[i]-1];r[i+k] == r[j+k];++k) {
33         }
34     }
35 }
36 int main() {
37     int n;
38     while(scanf("%d",&n)&&n) {
39         for(int i = 0; i < n; i ++) scanf("%d",&r[i]);
40         for(int i = 0; i < n-1; i ++) r[i] = r[i+1] - r[i] + 100;
41         r[--n] = 0;
42         da(r,sa,n+1,200);
43         calheight(r,sa,n);
44         int l = 1, r = n+1, m, L, R;
45         bool flag = false;
46         while(l <= r) {
47             m = (l+r)>>1;
48             L = INF, R = -INF,flag=false;
49             for(int i = 1; i <= n; i ++) {
50                 if(height[i] >= m) {
51                     L = min(L,sa[i]);
52                     L = min(L,sa[i-1]);
53                     R = max(R,sa[i]);
54                     R = max(R,sa[i-1]);
55                 }else {
56                     if(L+m+1 <= R) flag = true;
57                     L = INF, R = -INF;
58                 }
59             }
60             if(L+m+1 <= R) flag = true;
61             if(flag) l = m+1;
62             else r = m-1;
63         }
64         if(l >= 5)printf("%d\n",l);
65         else printf("0\n");
66     }
67     return 0;
68 }

 

转载于:https://www.cnblogs.com/xingkongyihao/p/7261747.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值