P2852 [USACO06DEC]Milk Patterns

题意

显然如果有一个子串出现过\(k\)次,那么它必定是一个至少长为k的后缀序的\(LCP\),求出所有相邻的长为\(k-1\)的\(height\)数组的最小值,在其中取最大值即可

code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=20010;
const int maxm=1000010;
int n,m,num,ans;
int a[maxn],sa[maxn],rk[maxn],oldrk[maxn],id[maxn],tmpid[maxn],cnt[maxm],height[maxn];
multiset<int>s;
inline bool check(int x,int y,int k){return oldrk[x]==oldrk[y]&&oldrk[x+k]==oldrk[y+k];}
inline void sa_build()
{
    num=1000000;
    for(int i=1;i<=n;i++)cnt[rk[i]=a[i]]++;
    for(int i=1;i<=num;i++)cnt[i]+=cnt[i-1];
    for(int i=n;i;i--)sa[cnt[rk[i]]--]=i;
    for(int t=1;t<n;t<<=1)
    {
        int tot=0;
        for(int i=n-t+1;i<=n;i++)id[++tot]=i;
        for(int i=1;i<=n;i++)if(sa[i]>t)id[++tot]=sa[i]-t;
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=n;i++)cnt[tmpid[i]=rk[id[i]]]++;
        for(int i=1;i<=num;i++)cnt[i]+=cnt[i-1];
        for(int i=n;i;i--)sa[cnt[tmpid[i]]--]=id[i];
        memcpy(oldrk,rk,sizeof(rk));
        tot=0;
        for(int i=1;i<=n;i++)rk[sa[i]]=check(sa[i-1],sa[i],t)?tot:++tot;
        num=tot;
    }
    for(int i=1,j=0;i<=n;i++)
    {
        if(j)j--;
        while(a[i+j]==a[sa[rk[i]-1]+j])j++;
        height[rk[i]]=j;
    }
}
int main()
{
    scanf("%d%d",&n,&m);m--;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    sa_build();
    for(int i=1;i<=n;i++)
    {
        s.insert(height[i]);
        if(i>m)s.erase(s.find(height[i-m]));
        if(i>=m)ans=max(ans,*s.begin());
    }
    printf("%d",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/nofind/p/12051558.html

时间: 2024-08-30 17:25:21

P2852 [USACO06DEC]Milk Patterns的相关文章

[USACO06DEC] Milk Patterns

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 pattern

[BZOJ1717][Usaco2006 Dec]Milk Patterns 产奶的模式

1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1297  Solved: 705 [Submit][Status][Discuss] Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个"模式". John的牛奶按质量可以被赋予一个0到100

POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次

Milk Patterns 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 r

POJ 3261 Milk Patterns

Milk Patterns Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 326164-bit integer IO format: %lld      Java class name: Main Farmer John has noticed that the quality of milk given by his cows varies from day t

BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]

1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1017  Solved: 561[Submit][Status][Discuss] Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个“模式”. John的牛奶按质量可以被赋予一个0到1000000之间的

poj 3261 Milk Patterns 后缀数组+二分

1 /*********************************************************** 2 题目: Milk Patterns(poj 3261) 3 链接: http://poj.org/problem?id=3261 4 题意: 给一串数字,求这些数字中公共子串个数大于k的 5 最长串. 6 算法: 后缀数组+二分 7 ***********************************************************/ 8 #incl

POJ 3261 Milk Patterns sa+二分

Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11944   Accepted: 5302 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,

Milk Patterns

poj3261:http://poj.org/problem?id=3261 题意: 题解: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn=20010; 6 char str[maxn]; 7 int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[maxn]; 8 int

POJ 3261 Milk Patterns (求可重叠的k次最长重复子串)

Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14094   Accepted: 6244 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,