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

题意:

找一个最长的子串,他在原字符串中出现的次数至少k次。允许重叠。

思路:

二分枚举答案。对某一段i~j,如果height[i~j]都超过了mid,说明可以有一个长度为mid的串在这些后缀中都有出现过。也就是说这个串出现了j - i次,统计这个次数,如果超过k,st = mid + 1

感觉应该hash也能做?没写过。【嗯不行不行,仔细想了一下hash好像要n方】

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = 1e6 + ;
int n, k;
char str[maxn]; int sa[maxn];
int t1[maxn], t2[maxn], c[maxn];
LL rnk[maxn], height[maxn];
int cnt[maxn]; void build_sa(int s[], int n, int m)
{
int i, j, p, *x = t1, *y = t2;
for(i = ; i < m; i++)c[i] = ;
for(i = ; i < n; i++)c[x[i] = s[i]]++;
for(i = ; i < m; i++)c[i] += c[i - ];
for(i = n - ; i >= ; i--)sa[--c[x[i]]] = i;
for(j = ; j <= n; j <<= ){
p = ;
for(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 < m; i++)c[i] = ;
for(i = ; i < n; i++)c[x[y[i]]]++;
for(i = ; i < m; i++)c[i] += c[i - ];
for(i = n - ; i >= ; i--)sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = ;
x[sa[]] = ;
for(i = ; i < n; i++)
x[sa[i]] = y[sa[i - ]] == y[sa[i]] && y[sa[i - ] + j] == y[sa[i] + j] ? p - :p++;
if(p >= n)break;
m = p;
}
} void get_height(int s[], int n)
{
int i, j, k = ;
//cout<<"SA:"<<endl;
for(i = ; i <= n; i++){
//cout<<sa[i]<<endl;
rnk[sa[i]] = i;
}
for(i = ; i < n; i++){
if(k) k--;
j = sa[rnk[i] - ];
while(s[i + k] == s[j + k])k++;
height[rnk[i]] = k;
}
} bool check(int t)
{
int num = ;
for(int i = ; i <= n; i++){
if(height[i] >= t){
num++;
if(num >= k)return true;
}
else num = ;
}
return false;
} int s[maxn];
int main()
{
while(scanf("%d%d", &n, &k) != EOF){
//scanf("%s", str);
int m = -inf;
for(int i = ; i < n; i++){
scanf("%d", &s[i]);
m = max(m, s[i]);
cnt[i] = ;
}
s[n] = cnt[n] = ;
build_sa(s, n + , m + );
//cout<<1<<endl;
get_height(s, n);
//cout<<2<<endl; int st = , ed = n, ans;
while(st <= ed){
int mid = (st + ed) / ;
if(check(mid)){
st = mid + ;
ans = mid;
}
else{
ed = mid - ;
}
} printf("%d\n", ans); }
return ;
}

最新文章

  1. win下修改mysql默认的字符集以防止乱码出现
  2. javascript面试题(一)
  3. Java开源GIS系统
  4. svnChina的使用方法
  5. 组态王6.55WEB全新发布步骤
  6. 使用fastcgi_cache加速网站
  7. 对discuz的代码分析学习(三)mysql驱动
  8. BZOJ 1820: [JSOI2010]Express Service 快递服务( dp )
  9. ThinkPhp学习08
  10. FZU 1920 Left Mouse Button 简单搜索
  11. Email:2017
  12. UDP接收百万级数据的解决方案
  13. Python_os、os.path、os.shutil使用案例
  14. jquery-Ajax请求用例码
  15. 计算机信息类ComputerInfo(车)
  16. mysql限制用户只能访问指定数据库
  17. ArrayList和LinkedList的区别以及优缺点
  18. windows下svn钩子实现每次提交更新至web目录
  19. dependencies、devDependencies、webpack打包 的区别与联系
  20. tenda t402 家庭版 有线路由器

热门文章

  1. 手机APP支付--整合银联支付控件
  2. oracle sqlplus常用命令大全
  3. Linux+Redis实战教程_day03_Redis-set【重点】_有序set(了解)
  4. 5 -- Hibernate的基本用法 --2 Hibernate入门
  5. lua 根据函数名字符串来执行函数
  6. 编译poco-1.7.8
  7. 获取预制和获取gameObject
  8. TableView头视图高度问题
  9. iOS - UITableViewStylePlain与UITableViewStyleGroup样式的对比
  10. C#实现HTTP请求文件下载,GET、POST请求的数据流接收