Milk Patterns

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

Source

 
 //2017-08-11
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int N = ;
const int inf = 0x3f3f3f3f;
char str[N];
int n, r[N], k;
int wa[N], wb[N], wv[N], wss[N];
int Suffix[N];//Str下标为i ~ Len的连续子串(即后缀)
int SA[N];//满足Suffix[SA[1]] < Suffix[SA[2]] …… < Suffix[SA[Len]],即排名为i的后缀为Suffix[SA[i]](与Rank是互逆运算)
int Rank[N];//Suffix[i]在所有后缀中的排名
int Height[N];//height[i]表示Suffix[SA[i]]和Suffix[SA[i-1]]的最长公共前缀,也就是排名相邻的两个后缀的最长公共前缀
int H[N];//等于Height[Rank[i]],也就是后缀Suffix[i]和它前一名的后缀的最长公共前缀 //比较母串r中起始位置为a和b,长度都为len的子串是否相等
int cmp(int *r, int a, int b, int len)
{
return r[a]==r[b] && r[a+len]==r[b+len];
} //倍增算法求SA数组。
void da(int *r, int *SA, int n, int m)
{
int i, j, p, *x = wa, *y = wb, *t;
for(i = ; i < m; i++)wss[i] = ;
for(i = ; i < n; i++)wss[x[i]=r[i]]++;
for(i = ; i < m; i++)wss[i]+=wss[i-];
for(i = n-; i >= ; i--)SA[--wss[x[i]]]=i;
for(j = , p = ; p < n; j *= , m = p){
for(p = , i = n-j; i < n; i++)
y[p++] = i;
for(i = ; i < n; i++)
if(SA[i] >= j)
y[p++] = SA[i]-j;
for(i = ; i < n; i++)
wv[i] = x[y[i]];
for(i = ; i < m; i++)
wss[i] = ;
for(i = ; i < n; i++)
wss[wv[i]]++;
for(i = ; i < m; i++)
wss[i] += wss[i-];
for(i = n-; i >= ; i--)
SA[--wss[wv[i]]] = y[i];
for(t = x, x = y, y = t, p = , x[SA[]]=, i = ; i < n; i++)
x[SA[i]] = cmp(y, SA[i-], SA[i], j)?p-:p++;
}
} //计算height数组
void cal_Height(int *r, int *SA, int n)
{
int i, j, k = ;
for(i = ; i <= n; i++)Rank[SA[i]] = i;
for(i = ; i < n; Height[Rank[i++]] = k)
for(k?k--:, j=SA[Rank[i]-]; r[i+k]==r[j+k]; k++)
;
} int st[N][]; void init_rmq(int n)
{
for(int i=;i<=n;i++) st[i][]=Height[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
{
st[i][j]=min(st[i][j-],st[i+(<<(j-))][j-]);
}
} //询问后缀i和后缀j的最长公共前缀
int lcp(int i,int j)
{
i = Rank[i];
j = Rank[j];
if(i>j) swap(i,j);
i++;
int k=;
while(i+(<<(k+)) <= j) k++;
return min(st[i][k],st[j-(<<k)+][k]);
} bool check(int len){
int cnt = ;
for(int i = ; i <= n; i++){
if(Height[i] >= len){//将Height进行分组,每组内Height值均大于k。
cnt++;
}else cnt = ;//重新分组
if(cnt >= k)return true;//一组内元素个数不小于k,说明存在
}
return false;
} int main()
{
while(scanf("%d%d", &n, &k)!=EOF)
{
for(int i = ; i < n; i++)
scanf("%d", &r[i]);
da(r, SA, n+, );
cal_Height(r, SA, n);
//二分答案,进行判定
int l = , r = n, mid, ans = ;
while(l <= r){
mid = (l+r)/;
if(check(mid)){
ans = mid;
l = mid+;
}else r = mid-;
}
printf("%d\n", ans);
} return ;
}

POJ3261(SummerTrainingDay10-G 后缀数组)的更多相关文章

  1. poj3261 Milk Patterns 后缀数组求可重叠的k次最长重复子串

    题目链接:http://poj.org/problem?id=3261 思路: 后缀数组的很好的一道入门题目 先利用模板求出sa数组和height数组 然后二分答案(即对于可能出现的重复长度进行二分) ...

  2. POJ3261 Milks patterns(后缀数组)

    Farmer John has noticed that the quality of milk given by his cows varies from day to day. On furthe ...

  3. POJ3261 Milk Patterns —— 后缀数组 出现k次且可重叠的最长子串

    题目链接:https://vjudge.net/problem/POJ-3261 Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Tot ...

  4. POJ-3261 Milk Patterns,后缀数组+二分。。

                                                        Milk Patterns 题意:求可重叠的至少重复出现k次的最长的字串长. 这题的做法和上一题 ...

  5. G 唐纳德与子串(easy)(华师网络赛---字符串,后缀数组)(丧心病狂的用后缀自动机A了一发Easy)

    Time limit per test: 1.0 seconds Memory limit: 256 megabytes 子串的定义是在一个字符串中连续出现的一段字符.这里,我们使用 s[l…r] 来 ...

  6. [USACO07DEC]Best Cow Line G 字符串hash || 后缀数组

    [USACO07DEC]Best Cow Line G [USACO07DEC]Best Cow Line G 小声哔哔:字符串hash牛逼 题意 给出一个字符串,每次可以从字符串的首尾取出一个字符, ...

  7. POJ3261 Milk Patterns 【后缀数组】

    牛奶模式 时间限制: 5000MS   内存限制: 65536K 提交总数: 16796   接受: 7422 案件时间限制: 2000MS 描述 农夫约翰已经注意到,他的牛奶的质量每天都在变化.经进 ...

  8. POJ3261(后缀数组+2分枚举)

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12972   Accepted: 5769 Ca ...

  9. 【BZOJ1717&POJ3261】Milk Patterns(后缀数组,二分)

    题意:求字符串的可重叠的k次最长重复子串 n<=20000 a[i]<=1000000 思路:后缀数组+二分答案x,根据height分组,每组之间的height>=x 因为可以重叠, ...

  10. POJ 2774 Long Long Message 后缀数组

    Long Long Message   Description The little cat is majoring in physics in the capital of Byterland. A ...

随机推荐

  1. poj1029 False coin

    http://poj.org/problem?id=1029 题目大意:“金条”银行从可靠的消息来源得知,在他们最后一组N个硬币中,一枚硬币是假的,与其他硬币的重量不同(其他硬币的重量相等).在经济危 ...

  2. ubuntu14中把openssh升级到7.5

    经验证可用 下载相关tar.gz包,没有下载到的,可以在这里下载.http://download.csdn.net/download/songanshu/10023663 apt install -y ...

  3. 设计模式《JAVA与模式》之状态模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述状态(State)模式的: 状态模式,又称状态对象模式(Pattern of Objects for States),状态模式是对象的行为 ...

  4. Shell - 简明Shell入门13 - 用户输入(UserInput)

    示例脚本及注释 1 - arguments #!/bin/bash if [ -n "$1" ];then # 验证参数是否传入 echo "The first para ...

  5. Windows Phone开发手记-WinRT下启动器替代方案

    在WP7/8时代,Silverlight框架提供了很多启动器API,我们可以很方便的使用的,来完成一些系统级的操作.但是随着Win RT架构的WP8.1(SL 8.1除外)的到来,原有的SL下的启动器 ...

  6. 【spring揭秘】1、关于IOC的基础概念

    1.基础概念 IOC有三种注入方式: 1.构造方法注入,就是通过构造方法进行实例化成员属性对象,优点是实现对象之后直接就可以使用,但是参数过多也是个麻烦 2.setter方法注入,实现相应的sette ...

  7. js 键盘事件

    <script type="text/javascript" language=JavaScript charset="UTF-8"> docume ...

  8. Andrew Ng机器学习课程笔记(三)之正则化

    Andrew Ng机器学习课程笔记(三)之正则化 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7365475.html 前言 ...

  9. springMVC上传文件简单案例

    html <form name="Form2" action="/SpringMVC006/fileUpload2" method="post& ...

  10. 通过HttpServletRequest获取服务器路径

    String scheme = request.getScheme(); String serverName = request.getServerName(); int port = request ...