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. [ActionScript 3.0] as3处理xml的功能和遍历节点

    as3比as2处理xml的功能增强了N倍,获取或遍历节点非常之方便,类似于json对像的处理方式. XML 的一个强大功能是它能够通过文本字符的线性字符串提供复杂的嵌套数据.将数据加载到 XML 对象 ...

  2. lucene使用与优化

    lucene使用与优化 1 lucene简介 1.1 什么是lucene Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么拿 ...

  3. 设计模式《JAVA与模式》之命令模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述命令(Command)模式的: 命令模式属于对象的行为模式.命令模式又称为行动(Action)模式或交易(Transaction)模式. ...

  4. 关于SQL的常用操作(增、删、改、查)

    关于SQL的常见操作主要是增.删.改.查. 1.增,顾名思义就是新增数据(insert into).该语句用于向表中插入新纪录.insert into有两种用法. (1).无需指定要插入数据的列名,只 ...

  5. Smart/400开发上手4: 调试Cobol代码 (DEBUG with QBATCH)

    Step1:Compile Cobol source CB TIM07 using *SRCDBG option例如:CB MEMBER(TIM07) OPTION(*SRCDBG) WORKU(TI ...

  6. h5文字超出,两行显示,超出显示省略号

    最近接到一个需求,要求商场导航里的文字最多显示两行,超出两行的省略号显示,查一些资料,又根据自己的需求,改了很多,直接上代码吧 <html> <head> <style ...

  7. 两台linux主机使用unison + inotify实现web文件夹同步

    两台服务器同步数据 unison 是一款跨平台的文件同步对象,不仅支撑本地对本地同步,也支持通过SSH,RSH和Socket 等网络协议进行同步. unison 支持双向同步,你可以同A同步到B ,也 ...

  8. MATLAB下数组随机打乱顺序的方法

    一:问题 有两个规模相同的数组,两个数组相同位置的元素一一对应,现在要将两数组的元素同时打乱顺序,并且乱序后的两数组对应位置元素要保持乱序前的对应关系. 二:方法  采用randperm()函数,产生 ...

  9. matlab中元胞数组的创建与内容读取

    一.创建元胞数组 1.用cell命令创建规格为2*2的空元胞 >> a=cell(2,2) a = [] [] [] [] 2.用大括号"{}"创建元胞数组并赋值 &g ...

  10. (转)面向对象(深入)|python描述器详解

    原文:https://zhuanlan.zhihu.com/p/32764345 https://www.cnblogs.com/aademeng/articles/7262645.html----- ...