POJ 1743 Musical Theme (the suffix array is used to find the longest repeated substring that cannot be overlapped)

Source: Internet
Author: User

Question: give some notes and find the longest recurring rotation length.

 


From the meaning of the question, we can know that as long as the adjacent difference is equal, we can create an array that is not adjacent. The question requires the longest length of the compound substring, and cannot overlap.

Because the adjacent difference may be negative, add 100 in a uniform way and convert it to the number between 0 and.

Then obtain the height array, which indicates the longest common prefix of the adjacent suffix.

Second answer, and then determine

You can group the Suffixes by height. If the height is greater than the length, you need to determine whether the suffixes overlap. Use the sa difference value. For details, see the code.


[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <queue>
# Include <cmath>
# Include <string>
# Include <vector>
# Include <algorithm>
# Include <map>
# Include <set>
# Define maxn50005
# Define eps 1e-8
# Define zero (a) fabs (a) <eps
Using namespace std;
// Obtain the suffix Array Using the multiplication algorithm.
Int wa [maxn], wb [maxn], wv [maxn], Ws [maxn];
Int cmp (int * r, int a, int B, int l)
{Return r [a] = r [B] & r [a + l] = r [B + l];}
Void da (const int * r, int * sa, int n, int m ){
Int I, j, p, * x = wa, * y = wb, * t;
For (I = 0; I <m; I ++) Ws [I] = 0;
For (I = 0; I <n; I ++) Ws [x [I] = r [I] ++;
For (I = 1; I <m; I ++) Ws [I] + = Ws [I-1];
For (I = n-1; I> = 0; I --) sa [-- Ws [x [I] = I;
For (j = 1, p = 1; p <n; j * = 2, m = p ){
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];
For (I = 0; I <m; I ++) Ws [I] = 0;
For (I = 0; I <n; I ++) Ws [wv [I] ++;
For (I = 1; I <m; I ++) Ws [I] + = Ws [I-1];
For (I = n-1; I> = 0; I --) sa [-- Ws [wv [I] = y [I];
For (t = x, x = y, y = t, p = 1, x [sa [0] = 0, I = 1; I <n; I ++)
X [sa [I] = cmp (y, sa [I-1], sa [I], j )? P-1: p ++;
}
Return;
}
Int sa [maxn], Rank [maxn], height [maxn];
// Calculate the height Array
Void calheight (const int * r, int * sa, int n ){
Int I, j, k = 0;
For (I = 1; I <= n; I ++) Rank [sa [I] = I;
For (I = 0; I <n; height [Rank [I ++] = k)
For (k? K --: 0, j = sa [Rank [I]-1]; r [I + k] = r [j + k]; k ++ );
Return;
}
Int n;
Int a [maxn], B [maxn];
Bool check (int m ){
Int mmax = 0, mmin = n;
For (int I = 1; I <= n; I ++ ){
If (height [I] <m ){
Mmax = sa [I];
Mmin = sa [I];
}
Else {
Mmax = max (mmax, max (sa [I], sa [I-1]);
Mmin = min (mmin, min (sa [I], sa [I-1]);
If (mmax-mmin> m) return true;
}
}
Return false;
}
Int main (){
While (scanf ("% d", & n )! = EOF & n ){
For (int I = 0; I <n; I ++) scanf ("% d", & a [I]);
For (int I = 0; I <n-1; I ++) B [I] = a [I + 1]-a [I] + 100;
N --;
B [n] = 0;
Da (B, sa, n + 1,200 );
Calheight (B, sa, n );
If (! Check (4 )){
Printf ("0 \ n ");
Continue;
}
Int ans, low = 0, high = n, mid;
While (low <= high ){
Mid = (low + high)/2;
If (check (mid )){
Ans = mid;
Low = mid + 1;
}
Else
High = mid-1;
}
Printf ("% d \ n", ans + 1 );
}
Return 0;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.