poj 3261

Milk Patterns

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 13249   Accepted: 5894
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

二分答案

#include<cstdio>
#include<cstring>
#include<algorithm>
#define MN 200003
using namespace std;

int n,m;
int s[MN],a[MN];
int v[MN],sa[MN],q[MN],rank[MN],h[MN],mmh=0;
bool w[101];
inline void gr(int x){
    rank[sa[1]]=1;
    for (int i=2;i<=n;i++) rank[sa[i]]=(s[sa[i]]==s[sa[i-1]]&&s[sa[i]+x]==s[sa[i-1]+x])?rank[sa[i-1]]:rank[sa[i-1]]+1;
    for (int i=1;i<=n;i++) s[i]=rank[i];
}
inline void gv(){memset(v,0,sizeof(v));for (int i=1;i<=n;i++) v[s[i]]++;for (int i=1;i<=2e5;i++)v[i]+=v[i-1];}
inline void gsa(){
    gv();for (int i=n;i>=1;i--) sa[v[s[i]]--]=i;gr(0);
    for (int i=1;i<n;i<<=1){
        gv();for (int j=n;j>=1;j--) if (sa[j]>i) q[v[s[sa[j]-i]]--]=sa[j]-i;
        for (int j=n-i+1;j<=n;j++) q[v[s[j]]--]=j;
        for (int j=1;j<=n;j++) sa[j]=q[j];gr(i);
        if (rank[sa[n]]==n) return;
    }
}
inline void gh(){for (int i=1,k=0,j;i<=n;h[rank[i++]]=k) for (k?k--:0,j=sa[rank[i]-1];a[i+k]==a[j+k]&&i+k<=n&&j+k<=n;k++);}
int main(){
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]),s[i]=a[i];
    gsa();gh();
    int l=0,r=n,mid,i,j,k,mmh;
    while(l<r){
        mid=(l+r+1)>>1;
        for (i=1,j,k=2;i<=n;i=k++){
            memset(w,0,sizeof(w));
            while (h[k]>=mid&&k<=n) k++;
            if (k-i>=m) break;
        }
        if (i<=n) l=mid;else r=mid-1;
    }
    printf("%d\n",l);
}

1668K 94MS G++ 1288B


 
时间: 2024-08-23 12:52:49

poj 3261的相关文章

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 后缀数组 找重复出现k次的子串(子串可以重叠)

题目:http://poj.org/problem?id=3261 仍然是后缀数组的典型应用----后缀数组+lcp+二分 做的蛮顺的,1A 但是大部分时间是在调试代码,因为模板的全局变量用混了,而自己又忘了,,,等西安邀请赛还有四省赛结束之后,该冷静反思下尝试拜托模板了 错误   :1.k用错,题目的k和模板的k用混; 2.还是二分的C()函数,这个其实跟前一篇<poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题>的C函数

poj 3261 后缀数组 找反复出现k次的子串(子串能够重叠)

题目:http://poj.org/problem?id=3261 仍然是后缀数组的典型应用----后缀数组+lcp+二分 做的蛮顺的,1A 可是大部分时间是在调试代码.由于模板的全局变量用混了,而自己又忘了.,,等西安邀请赛还有四省赛结束之后,该冷静反思下尝试拜托模板了 错误   :1.k用错,题目的k和模板的k用混; 2.还是二分的C()函数,这个事实上跟前一篇<poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题>的C函

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,

[poj 3261]后缀数组+滑窗最小值

题目链接:http://poj.org/problem?id=3261 这个是可以交叉的重复串,所以用height就可以了,但是题目说让重复k次以上,也就是直接做一个k-1长度的滑窗最小值,从这些最小值里取最大即可. 这里其实为了节省空间可以先给数字离散化一下,这样就只有20000了,不过不离散化空间也够用. #include<cstdio> #include<algorithm> #include<cstring> #include<queue> usin

POJ 3261 Milk Patterns(后缀数组+二分答案)

[题目链接] http://poj.org/problem?id=3261 [题目大意] 求最长可允许重叠的出现次数不小于k的子串. [题解] 对原串做一遍后缀数组,二分子串长度x,将前缀相同长度超过x的后缀分组, 如果存在一个大小不小于k的分组,则说明答案可行,分治得到最大可行解就是答案. [代码] #include <cstdio> #include <cstring> #include <vector> using namespace std; const int

POJ 3261 Milk Patterns 可重复k次的最长重复子串

Milk PatternsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3261 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 c

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

后缀数组 POJ 3261 Milk Patterns

题目链接 题意:可重叠的 k 次最长重复子串.给定一个字符串,求至少出现 k 次的最长重复子串,这 k 个子串可以重叠. 分析:与POJ 1743做法类似,先二分答案,height数组分段后统计 LCP>=m 的子串的个数. #include <cstdio> #include <cstring> #include <algorithm> const int N = 2e4 + 5; int sa[N], rank[N], height[N]; int t[N],

POJ - 3261 Milk Patterns (后缀数组求可重叠的 k 次最长重复子串)

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