POJ 3261 Milk Patterns(尾碼數組 重複出現K次字串的長度)

來源:互聯網
上載者:User
Milk Patterns
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 7956   Accepted: 3610
Case Time Limit: 2000MS

Description

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

To perform a rigorous study, he has invented a complex classification scheme by which each milk sample is recorded as an integer between 0 and 1,000,000 inclusive, and has 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 ≤ K ≤ N) times. This may 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 that at least one subsequence is repeated at least K times.

Input

Line 1: Two space-separated integers: N and K 
Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.

Output

Line 1: One integer, the length of the longest pattern which occurs at least K times

Sample Input

8 212323231

Sample Output

4

Source

USACO 2006 December Gold

這個題目和上一個題目基本類似,上一個題目是要求求一個最長不重複字串,這個題目要求的是求至少重複K次的一個字串的最長長度

思想和上一個題目的思想基本類似,同樣是二分同樣是height區間分組,上一個題目在給區間分組的時候是按照height的值來分組,這個題目

也是,不過不同的是上一個題目要求記錄出的是一個分組好了的區間的一個最左邊和最右邊方便判斷是否重複,這個記錄區間最大的長度

這樣就能保證最大出現的次數,本質上課上面那個題目是一樣的,二分分組

#include <iostream>#include <stdio.h>#include <algorithm>#include <math.h>using namespace std;#define maxn 1021000#define ws ws1int 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(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 rank[maxn],height[maxn];void calheight(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 rec[maxn],sa[maxn];int n,len;bool check(int m){    int count=0;    for(int i=1;i<=n;i++)    {        if(height[i]<m)        {            count=0;        }        else        {            count++;            if(count >= len-1)            return true;        }    }    return false;}int main(){    int i,j,k;    int left,right;    int mid;    int answer;    while(scanf("%d%d",&n,&len)!=EOF)    {     for(i=0;i<n;i++)     scanf("%d",&rec[i]);     rec[n]=0;     da(rec,sa,n+1,1000011);//這裡是n+1 因為看這個函數裡面是 < n 的     calheight(rec,sa,n);//注意這裡面是 n 了因為看函數裡面是 <=n 的,所以這裡要注意     /*for(i=0;i < n ;i++)     printf("%d ",height[i]);     printf("\n");*/     if(!check(1))     {         printf("0\n");         continue;     }     left=1;     right=20000;     answer=-1;     while(left <= right)     {         mid=(left+right)>>1;         if(check(mid))         {             answer=mid;             left=mid+1;         }         else         {             right=mid-1;         }     }     printf("%d\n",answer);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.