POJ 3261 Milk Patterns suffix array to find a string of the longest repeatable substring repeat at least k times

Source: Internet
Author: User

Milk Patterns

Description

Farmer John has noticed, the quality of milk given by he cows varies from day to day. On further investigation, he discovered so although he can ' t predict the quality of milk from one day to the next, there is some regular patterns in the daily milk quality.

To perform a rigorous study, he had invented a complex classification scheme by which each milk sample was recorded as an I Nteger between 0 and 1,000,000 inclusive, and have recorded data from a single cow over n (1≤n ≤20,000) Days. He wishes to find the longest pattern of samples which repeats identically at least K (2≤ kN) times. This could include overlapping patterns--1 2 3 2 3 2 3 1 repeats 2 3 2 3 twice, for example.

Help Farmer John by finding the longest repeating subsequence in the sequence of samples. It is guaranteed, at least one subsequence are repeated at least K times.

Input

Line 1:two space-separated integers: Nand K
Lines 2.. N+ 1: NIntegers, one per line, the quality of the milk on day IAppears on the ITh line.

Output

Line 1:one Integer, the length of the longest pattern which occurs at least KTimes

Sample Input

8 212323231

Sample Output

4

Remember the discretization of

#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<vector>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")#defineLS i<<1#defineRS ls | 1#defineMid ((LL+RR) >>1)#definePII pair<int,int>#defineMP Make_pairtypedefLong LongLL;Const Long LongINF = 1e18+1LL;Const DoublePi = ACOs (-1.0);Const intN = 2e5+Ten, M = 2e5+ -, mod = 1e9+7, INF =2e9;///Heght[i] represents the longest common prefix for Suffix (sa[i-1]) and Suffix (Sa[i]):///Rank[i] Indicates the rank of the suffix that begins with I:///Sa[i] Indicates the beginning of the suffix that is ranked I:int*Rank,r[n],sa[n],height[n],wa[n],wb[n],wm[n];BOOLcmpint(RNintAintBintl) {returnR[a] = = R[b] && r[a+l] = = r[b+l];}voidSA (int(RNint*sa,intNintm) {int*x=wa,*y=wb,*T;  for(intI=0; i<m;++i) wm[i]=0;  for(intI=0; i<n;++i) wm[x[i]=r[i]]++;  for(intI=1; i<m;++i) wm[i]+=wm[i-1];  for(inti=n-1; i>=0;-I.) sa[--wm[x[i]]]=i;  for(intI=0, j=1, p=0;p <n;j=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]&GT;=J) y[p++]=sa[i]-J;  for(i=0; i<m;++i) wm[i]=0;  for(i=0; i<n;++i) wm[x[y[i]]]++;  for(i=1; i<m;++i) wm[i]+=wm[i-1];  for(i=n-1; i>=0;-I.) sa[--wm[x[y[i]]]]=Y[i];  for(t=x,x=y,y=t,i=p=1, x[sa[0]]=0; i<n;++i) {X[sa[i]]=CMP (y,sa[i],sa[i-1],j)? p1:p + +; }} Rank=x;}voidHeight (int(RNint*sa,intN) { for(intI=0, j=0, k=0; i<n;height[rank[i++]]=k) for(k?--K:0, j=sa[rank[i]-1];r[i+k] = = r[j+k];++k);}intn,a[n],k;intCheckintx) {inti =2, CNT;  while(1) {             while(I <= n && height[i] < x) i++; if(i > N) Break; CNT=1;  while(I <= n && height[i] >=x) {cnt++; I++; }            if(CNT >=k) {return 1; }        }        return 0;}intMain () { while(~SCANF ("%d%d",&n,&k)) { for(inti =1; I <= N; ++i) scanf ("%d", &a[i]), r[i-1] =A[i]; Sort (a+1, a+n+1); intc = Unique (A +1, a+n+1)-A-1;  for(inti =0; I < n; ++i) R[i] = Lower_bound (A +1, a+c+1, R[i])-A; R[n]=0; SA (R,sa,n+1,20001);            Height (R,sa,n); intLL =1, rr = N,ans =0;  while(LL <=RR) {                intMD = (ll + RR) >>1; if(Check (MD)) {ans= MD, LL = MD +1; } ElseRR = MD-1; } printf ("%d\n", ans); }        return 0;}

  

POJ 3261 Milk Patterns suffix array to find a string of the longest repeatable substring repeat at least k times

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.