POJ 1743 Musical Theme (the longest repeating substring suffix array)
There is a sequence of n (1 <= n <=20000) notes to represent a piece of music, each note is 1. 88 in the range of integers, now look for a repeating topic. A "subject" is a substring of the entire note sequence, which needs to meet the following conditions: 1. The length is at least 5 notes. 2. Repeated occurrences in the song (possibly by transpose, "transpose" means that each note in the subject sequence is added or subtracted from the same integer value). 3. Repeated occurrences of the same subject cannot have a public part.
First, the sequence is differential, so now the problem is converted: Given a string, the longest repeating substring, the two substrings cannot overlap.
Let's take a look at the longest overlapping repeating substring. First, a pair of extremely long repeating substrings must be represented as a two-suffix LCP. At the same time, any two suffix LCP must correspond to a pair of extremely long repeating substrings. They are full-shot relationships. Therefore, the longest overlapping repeating substring is the longest in the LCP of any pair of suffixes, and is obviously the maximum height.
How does this approach extend to the longest non-overlapping suffix? We can find that only when the distance of two suffixes is greater than their LCP can it be included in the calculation. In this case, we cannot guarantee that the maximum value in height must be the answer, and there may be multiple suffixes between the two suffixes of the optimal value. The procedure is two string length L, which divides the suffix array into groups, and the height within each group is greater than L. Record the smallest and most positioned string within the group to see if the distance between them is greater than the substring length. If so, then mid=l+1, otherwise mid=r.
#include <cstdio>#include <cstring>#include <algorithm>using namespaceStdConst intmaxn=4e4+5;intN, M, A[MAXN];int*x, *y, *t, WA[MAXN], WB[MAXN], WS[MAXN], WV[MAXN], SA[MAXN], HT[MAXN];intcmpint*r,intAintBintL) {returnr[a]==r[b]&&r[a+l]==r[b+l]; }voidDaint*r) {x=wa; y=wb; m=maxn; for(intI=0; i<m; ++i) ws[i]=0; for(intI=0; i<n; ++i) ++ws[x[i]=r[i]; for(intI=1; i<m; ++i) ws[i]+=ws[i-1]; for(intI=0; i<n; ++i) sa[--ws[r[i]]]=i;The //sa array must be sorted. intI, J, p=0; for(j=1; j<n&&p<n; j<<=1, m=p+1){//p represents how many different suffixes m should become p+1 in the current multiplication situation for(p=0, I=n-j; i<n; ++i) y[p++]=i; for(i=0; i<n; ++i)if(sa[i]>=j) Y[p++]=sa[i]-j; for(i=0; i<n; ++i) Wv[i]=x[y[i];//WV: The number of the second keyword in the row I, in the first keyword in row for(i=0; i<m; ++i) ws[i]=0; for(i=0; i<n; ++i) ++ws[x[i];//ws: The number of the first keywords ranked I, the total ranking of the range is how much for(i=1; i<m; ++i) ws[i]+=ws[i-1]; for(i=n-1; i>=0; I.) sa[--ws[wv[i]]]=y[i]; T=x; X=y; y=t; x[sa[0]]=1; for(p=1, i=1; i<n; ++i)//rank must start from 1 to differentiate between empty stringsx[sa[i]]=cmp (Y, Sa[i-1], Sa[i], j)? P:++p; } memset (HT,0,sizeof(HT)); for(i=0; i<n; ++i)--x[i]; p=0; for(i=0; i<n; HT[X[I++]]=P) {//Enumerate all suffixes from 1 to n in the original string if(!x[i])Continue; for(p?p--:0, J=sa[x[i]-1]; r[i+p]==r[j+p]&&i+p<n; ++P);//p = h[i]}return;}intMain () { while(~SCANF ("%d", &n) &&n) { for(intI=0; i<n; ++i) scanf ("%d", &a[i]); for(intI=0; i<n; ++i) A[i]=a[i+1]-a[i]+88; --n; Da (a);intL=0, R=n, K, flag; while(L<r) {k= (l+r) >>1; flag=0;intminm=sa[0], maxm=sa[0]; for(intI=1; i<=n; ++i) {if(ht[i]<k) {if(maxm-minm>k) flag=1; Minm=sa[i]; Maxm=sa[i];Continue; } minm=min (MINM, sa[i]); Maxm=max (MAXM, sa[i]); }if(flag) L=k+1;ElseR=k; } printf ("%d\n", l<5?0: l); }return 0;}
POJ 1743 Musical Theme (the longest repeating substring suffix array)