POJ3261:Milk Patterns(后缀数组)

来源:互联网 发布:中国产业生产率数据库 编辑:程序博客网 时间:2024/06/06 07:51

Milk Patterns
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 16380 Accepted: 7233Case 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次的子串。

思路:后缀数组(利用了相同前缀的后缀一定排在一起的性质)+二分答案即可,数据范围1e6离散化会好一点。

# include <iostream># include <cstdio># include <cstring># include <algorithm>#define LL long long#define N 20005using namespace std;int wa[N],wb[N],wsf[N],wv[N],sa[N];int Rank[N],height[N],s[N],ss[N], a[N],n,id[N],kk;char str1[N],str2[N];int cmp(int *r,int a,int b,int k){    return r[a]==r[b]&&r[a+k]==r[b+k];}void getsa(int *r,int *sa,int n,int m){    int i,j,p,*x=wa,*y=wb,*t;    for(i=0; i<m; i++)  wsf[i]=0;    for(i=0; i<n; i++)  wsf[x[i]=r[i]]++;    for(i=1; i<m; i++)  wsf[i]+=wsf[i-1];    for(i=n-1; i>=0; i--)  sa[--wsf[x[i]]]=i;    p=1;    j=1;    for(; 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++)  wsf[i]=0;        for(i=0; i<n; i++)  wsf[wv[i]]++;        for(i=1; i<m; i++)  wsf[i]+=wsf[i-1];        for(i=n-1; i>=0; i--)  sa[--wsf[wv[i]]]=y[i];        t=x;        x=y;        y=t;        x[sa[0]]=0;        for(p=1,i=1; i<n; i++)            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)? p-1:p++;    }}void getheight(int *r,int n){    int i,j,k=0;    for(i=1; i<=n; i++)  Rank[sa[i]]=i;    for(i=0; i<n; i++)    {        if(k)            k--;        else            k=0;        j=sa[Rank[i]-1];        while(r[i+k]==r[j+k])            k++;        height[Rank[i]]=k;    }}bool check(int x){    int cnt = 1;    for(int i=2; i<=n; ++i)    {        if(height[i]>=x)        {            ++cnt;            continue;        }        if(cnt>=kk) return true;        else cnt=1;    }    return cnt>=kk;}bool cmp2(int x, int y){    return ss[x] < ss[y];}int main(){    scanf("%d%d",&n,&kk);    int li = 0, imax = 0;    for(int i=0; i<n; ++i) scanf("%d",&ss[i]), id[i]=i;    sort(id, id+n, cmp2);    s[id[0]] = ++li;    for(int i=1; i<n; ++i)    {        if(ss[id[i]] != ss[id[i-1]]) ++li;        s[id[i]] = li;    }    s[n] = 0;    getsa(s,sa,n+1,li+1);    getheight(s,n);    int L=0, R=n;    while(L<=R)    {        int mid = L+R>>1;        if(check(mid)) L = mid+1;        else R = mid-1;    }    printf("%d\n",R);    return 0;}


阅读全文
0 0
原创粉丝点击