poj3261

Milk Patterns
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 9983   Accepted: 4495
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 2
1
2
3
2
3
2
3
1

Sample Output

4

可重叠的出现k次的最长重复子串,裸题

直接SA

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>

using namespace std;

int co[20011],sa[20011],rank[20011],nr[20011];
int s[20011],a[20011],b[20011],h[20011];
int xzq,i,n,k,mm,x;

void sort(int *a)
{
    int i,mn;
    if(mm>n)mn=mm;
    else mn=n;
    for(i=0;i<=mn;i++)co[i]=0;
    for(i=1;i<=n;i++)co[a[i]+1]++;
    for(i=1;i<=mn;i++)co[i]+=co[i-1];
    for(i=1;i<=n;i++)nr[i]=0;
    for(i=1;i<=n;i++){
        co[a[sa[i]]]++;
        nr[co[a[sa[i]]]]=sa[i];
    }
    for(i=1;i<=n;i++)sa[i]=nr[i];
}

void getrank()
{
    int i;
    x=0;
    for(i=1;i<=n;i++){
        if(i==1||a[sa[i]]!=a[sa[i-1]]||b[sa[i]]!=b[sa[i-1]])x++;
        rank[sa[i]]=x;
    }
}

void Suffix()
{
    int i,l,last,j,k;
    for(i=1;i<=n;i++){
        sa[i]=i;
        b[i]=0;
    }
    sort(a);
    getrank();
    l=1;
    while(x!=n){
        for(i=1;i<=n;i++){
            a[i]=rank[i];
            if(i+l<=n)b[i]=rank[i+l];
            else b[i]=0;
        }
        sort(b);
        sort(a);
        getrank();
        l*=2;
    }
    last=0;
    for(i=1;i<=n;i++){
        if(last)last--;
        if(rank[i]==1)continue;
        j=i;
        k=sa[rank[i]-1];
        while(j+last<=n&&k+last<=n&&s[j+last]==s[k+last])last++;
        h[rank[i]]=last;
    }
}

bool check(int x)
{
    int i,t;
    t=1;
    for(i=2;i<=n+1;i++){
        if(h[i]<x){
            if(i-t>=k)return true;
            t=i;
        }
    }
    return false;
}

void Work()
{
    int l,r,mid;
    l=1;
    r=n;
    while(l<=r){
        mid=(l+r)/2;
        if(check(mid))l=mid+1;
        else r=mid-1;
    }
    xzq=r;
}

int main()
{
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(a[i]>mm)mm=a[i];
        s[i]=a[i];
    }
    Suffix();
    Work();
    printf("%d
",xzq);
}
原文地址:https://www.cnblogs.com/applejxt/p/3865428.html