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. Linux 操作系统文件略解

    1.使用tree命令查看根目录的树结构 # tree -L 1 如果没有tree命令,可以使用yum进行安装 # yum -y install tree 执行命令后,即可看到根下一共有19个目录 . ...

  2. jquery中ajax的几种方式

    三种简写: $.get(URL,data,function(data,status,xhr),dataType) $(selector).post(URL,data,function(data,sta ...

  3. [原创]内网渗透JSP webSehll连接工具

    工具: JspShellExec编译: VS2012  C# (.NET Framework v2.0)组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http://qqhack8.b ...

  4. swiper4-vue 不使用loop,由最后一张跳到第一张

    <template> <div class="swiper-box"> <div class="swiper-container" ...

  5. Nutch的nutch-default.xml和regex-urlfilter.txt的中文解释

    nutch-default解释.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl&qu ...

  6. Windows server2012 IIs 8 自定义日志记录

    问题: 通过CDN加速的网站,记录日志时无法追踪源IP,日志的IP都为CDN节点ip. 分析: 1.在解析记录header时,CDN实际会把源IP以其它header的形式回传,如网宿为[Cdn-Src ...

  7. 字符、字符串和文本的处理之Char类型

    .Net Framework中处理字符和字符串的主要有以下这么几个类: (1).System.Char类 一基础字符串处理类 (2).System.String类 一处理不可变的字符串(一经创建,字符 ...

  8. mysql 主键和唯一索引的区别

    主键是一种约束,唯一索引是一种索引,两者在本质上是不同的. 主键创建后一定包含一个唯一性索引,唯一性索引并不一定就是主键. 唯一性索引列允许空值,而主键列不允许为空值. 主键列在创建时,已经默认为非空 ...

  9. Unity3D第一战:软件安装与代码调试

    1.软件的安装 Unity3D可以轻松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台的综合型游戏开发工具,功能非常强大. 最新版下载地址:http://unity3d.com/u ...

  10. 前端模块化之CommonJS,ES6,AMD,CMD

    最近在搞跨平台解决方案,讨论关于模块划分的问题以及如何尽量多的复用逻辑代码.于是就有了此文章,之前的博客也写过,不过由于主机商跑路,宝贵的资源也就没了,说多了都是泪~ 这里按模块化发展的历史回溯的时间 ...