Hiho 120 weeks suffix array one • Repeat Melody

Source: Internet
Author: User

Suffix array one • Repeat Melody time limit:5000msSingle Point time limit:1000msMemory Limit:256MBDescribe

Small hi usually a big hobby is playing the piano. We know that a musical melody is represented as a series of numbers consisting of N in length.

Little hi has practiced a lot of music and found that many of the works themselves contain the same melody. Melody is a continuous sequence of numbers, similar melodies can overlap in the original sequence. In 1 2 3 2 3 2 1, for example, 2 3 2 have occurred.

Little hi want to know what is the longest melody in a melody at least k times?

Tips on how to solve problems

Input

The first line is two integers n and K. 1≤n≤20000 1≤k≤n

Next there are N integers that represent the number of each tone. 1≤ Digital ≤100

Output

An integer line that represents the answer.

Sample input
8 212323231
Sample output
4
An array of "parse" suffixes
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<vector>#include<algorithm>using namespacestd;Const intN =20000+ -;intcmpint*r,intAintBintl) {    return(R[a]==r[b]) && (r[a+l]==r[b+l]);}//used to compare the first keyword with the second keyword,//The special place is, when preprocessing, r[n]=0 (less than the preceding characters)/*DA (aa,sa,n+1,200); Calheight (aa,sa,n);*/intWa[n],wb[n],ws[n],wv[n];intRank[n];//The rank of suffix i in sa[]intHeight[n];//Sa[i] and Sa[i-1] LCPintSa[n];//Sa[i] Indicates the subscript for the small suffix of the rank ivoidDA (int*r,int*sa,intNintM//here n is more than 1 of the input n, a manually added character used to avoid CMP time out of bounds{    inti,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;//preprocessing length is 1     for(j=1, p=1; p<n; j*=2, m=p)//The SA that has been calculated for the length of J, to find 2*j SAS    {         for(p=0, I=n-j; i<n; i++) y[p++]=i;//Special handling without a second keyword         for(i=0; i<n; i++)if(sa[i]>=j) Y[p++]=sa[i]-j;//using the length J, sort by the second keyword         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];//Base Sort Section         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)? p1:p + +;//update rank array x[], pay attention to the same    }}voidCalheight (int*r,int*sa,intN//here n is the actual length{    inti,j,k=0;//the legal range of height[] is 1-n, where 0 is the end-added character     for(i=1; i<=n; i++) rank[sa[i]]=i;//rank according to SA     for(i=0; i<n; height[rank[i++]] = k)//definition: h[i] = height[Rank[i]]         for(k?k--:0, j=sa[rank[i]-1]; R[I+K]==R[J+K]; k++);//optimize the calculation height process according to H[i] >= h[i-1]-1}intn,m;CharSs[n];intAa[n];intSolveintk) {    intans=0;  for(intI=1; i<=n; i++)    {        if(height[i]<k)Continue; intMaxx=max (sa[i-1],sa[i]), Minn=min (sa[i-1],sa[i]); intcun=2; intj=i;  while(height[j+1]>=k&&j<=n) j++,cun++; Ans=Max (Ans,cun); I=J; }    returnans>=m;}intMain () {scanf ("%d%d",&n,&m);  for(intI=0; i<n; i++) scanf ("%d",&Aa[i]); Aa[n]=0; DA (Aa,sa,n+1, -);    Calheight (Aa,sa,n); intL=0, r=n,ans=-1;;  while(l<=r) {intMid= (l+r) >>1; if(Solve (mid)) ans=mid,l=mid+1; Elser=mid-1; } printf ("%d\n", ans); return 0;}

Hiho 120 weeks suffix array one • Repeat Melody

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.