POJ 1743 musical Theme suffix array longest repeating disjoint substring

Source: Internet
Author: User

Musical Theme
Time Limit:20 Sec

Memory limit:256 MB

Topic Connection

http://poj.org/problem?id=1743

Description

A musical melody is represented as a sequence of N (1<=n<=20000) Notes this is integers in the range 1..88, each rep Resenting a key on the piano. It is unfortunate, but true, this representation of melodies ignores the notion of musical timing; But, the this programming task was 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 their other appearance (s)

Transposed means that a constant positive or negative value are 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 all test case contains the integer N. The following n integers represent the sequence of notes.
The last test case was 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 L Ongest theme. If There is 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

HINT

Test instructions

Lets you find the longest disjoint repeating substring

The following:

Suffix array, run out of the height array, then directly two minutes and then O (n), check just fine

Code:

//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 25000#defineMoD 10007#defineEPS 1e-9intNum;Charch[ -];//const int INF=0X7FFFFFFF; //нчоч╢сConst intinf=0x3f3f3f3f; inline ll read () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************ints[2*MAXN];intA[MAXN];intN;intSA[MAXN], RANK[MAXN], HEIGHT[MAXN];intWA[MAXN], WB[MAXN], WV[MAXN], WD[MAXN];intcmpint(RNintAintBintl) {    returnR[a] = = R[b] && r[a+l] = = r[b+l];}voidBuild_sa (int(RNintNintm) {//multiplication Algorithm R is a range of characters for which the array n to be matched is the total length m    intI, J, p, *x = WA, *y = WB, *T;  for(i =0; I < m; i + +) wd[i] =0;  for(i =0; I < n; i + +) Wd[x[i]=r[i]] + +;  for(i =1; I < m; i + +) Wd[i] + = wd[i-1];  for(i = n1; I >=0; I--) sa[--wd[x[i]] =i;  for(j =1, p =1; P < n; J *=2, M =p) {         for*pb=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 + +) wd[i] =0;  for(i =0; I < n; i + +) Wd[wv[i]] + +;  for(i =1; I < m; i + +) Wd[i] + = wd[i-1];  for(i = n1; I >=0; I--) sa[--wd[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 + +; }    }}voidCalheight (int*r,intN) {//The array of height is evaluated.     intI, 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 + +); }}intCheckintmid) {    intminn=sa[0],maxn=sa[0];  for(intI=0; i<=n;i++)    {        if(height[i]>=mid) {Minn=min (Sa[i],minn); MAXN=Max (SA[I],MAXN); if(maxn-minn>mid)return 1; }        ElseMinn=maxn=Sa[i]; }    return 0;}voidinit () {memset (A,0,sizeof(a)); memset (s),0,sizeof(s)); memset (SA,0,sizeof(SA)); memset (rank,0,sizeof(rank)); memset (height,0,sizeof(height)); memset (WA,0,sizeof(WA)); memset (WB,0,sizeof(WB)); memset (WV,0,sizeof(WV)); memset (WD,0,sizeof(wd));}intMain () { while(cin>>N) {if(n==0)             Break;        Init ();  for(intI=0; i<n;i++) A[i]=read (); N--;  for(intI=0; i<n;i++) S[i]=a[i+1]-a[i]+ -; S[n]=0; Build_sa (S,n+1, $);        Calheight (S,n); intL=0, r=n,ans=0;  while(l<=r) {intMid= (l+r) >>1; if(check (mid)) {ans=Max (ANS,MID); L=mid+1; }            ElseR=mid-1; }        if(ans<4) ans=0; Elseans++; cout<<ans<<Endl; }}

POJ 1743 musical Theme suffix array longest repeating disjoint substring

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.