Topic Links:

Poj 1743 Musical Theme

Title Description:

Give a string of numbers (the number range is in [1,88]), to find a topic in this string of numbers that satisfies:

1: Topic length is greater than or equal to 5.

2: The theme repeats in the text string (or, after a twist, the subject adds or subtracts the same integer)

3: Duplicate themes cannot overlap

Problem Solving Ideas:

To reverse the repeated occurrences of the substring, the difference between the themes must be constant. You can find the difference between two adjacent numbers in the text string s, re-form a new text string s, and then look for the longest public non-overlapping prefix in the s suffix string. Rank adjacent suffix string, the public prefix must be the longest, but it is possible to overlap. We can divide the length of the subject by K, and then verify whether K is set. According to the nature of height, the more similar suffix string rank difference is smaller, then we can find I=min (Sa[rank]) in the adjacent interval of Height[rank]>=k, J=max (Sa[rank]), if j-i>= K. K is established.

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6typedefLong LongLL;7 Const intMAXN =20010;8 9 intSA[MAXN], RANK[MAXN], HEIGHT[MAXN];Ten intT1[MAXN], T2[MAXN], R[MAXN], C[MAXN]; One BOOLCMP (int*STR,intAintBintk) A { - returnSTR[A]==STR[B] && str[a+k]==str[b+K]; - } the voidDa (int*STR,intNintm) - { -n + +; - int*x = T1, *y =T2, I, J; + for(i=0; i<m; i++) C[i] =0; - for(i=0; i<n; i++) C[x[i]=str[i] + +; + for(i=1; i<m; i++) C[i] + = c[i-1]; A for(i=n-1; i>=0; i--) sa[--C[x[i]] =i; at for(j=1; j<=n; j*=2) - { - intp =0; - for(i=n-j; i<n; i++) y[p++] =i; - for(i=0; i<n; i++)if(Sa[i] >= j) y[p++] = Sa[i]-J; - in for(i=0; i<m; i++) C[i] =0; - for(i=0; i<n; i++) C[x[y[i]] + +; to for(i=1; i<m; i++) C[i] + = c[i-1]; + for(i=n-1; i>=0; i--) sa[--C[x[y[i]] =Y[i]; - the swap (x, y); *p =1; $x[sa[0]] =0;Panax Notoginseng for(intI=1; i<n; i++)//i is rank -X[sa[i]] = cmp (y, sa[i-1], Sa[i], j)? p1:p + +; the if(P >=N) + Break; Am =p; the } + for(i=1; i<n; i++) -Rank[sa[i]] =i; $ intK =0; $N--; - for(intI=0; i<n; i++) - { the if(k) K--; - intj = Sa[rank[i]-1];Wuyi while(Str[i+k] = = Str[j+k]) k++; theHeight[rank[i]] =K; - } Wu } - BOOLSolve (intXintN) About { $ intMa, MI; -Ma = mi = sa[1]; - for(intI=2; i<=n; i++) - { A if(Height[i]>=x && i<=N) + { theMi =min (mi, sa[i]); -Ma =Max (MA, sa[i]); $ if(Ma-mi >= x)return true; the Continue; the } the theMa = mi =Sa[i]; - } in return false; the } the intMain () About { the intN; the while(SCANF ("%d", &N), N) the { + intS, E; -scanf ("%d", &s); the for(intI=1; i<n; i++)Bayi { thescanf ("%d", &e); ther[i-1] = S-e + -; -s =e; - } theR[--n] =0; the theDa (r, N, $); the intAns =0, high = n/2, low =1, mid; - while(Low <=High ) the { theMid = (low + high)/2; the if(Solve (Mid, n))94 { theAns =mid; theLow = mid +1; the }98 Else AboutHigh = mid-1; - }101printf ("%d\n", ans<4?0: ans+1);102 }103 return 0;104}

Poj 1743 musical Theme (suffix array + dichotomy)