博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3261 Milk Patterns【后缀数组】【二分】
阅读量:4356 次
发布时间:2019-06-07

本文共 3074 字,大约阅读时间需要 10 分钟。

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 212323231

Sample Output

4

 

题意:

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

思路:

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

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

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 using namespace std; 11 typedef long long LL; 12 #define inf 0x7f7f7f7f 13 14 const int maxn = 1e6 + 5; 15 int n, k; 16 char str[maxn]; 17 18 int sa[maxn]; 19 int t1[maxn], t2[maxn], c[maxn]; 20 LL rnk[maxn], height[maxn]; 21 int cnt[maxn]; 22 23 void build_sa(int s[], int n, int m) 24 { 25 int i, j, p, *x = t1, *y = t2; 26 for(i = 0; i < m; i++)c[i] = 0; 27 for(i = 0; i < n; i++)c[x[i] = s[i]]++; 28 for(i = 1; i < m; i++)c[i] += c[i - 1]; 29 for(i = n - 1; i >= 0; i--)sa[--c[x[i]]] = i; 30 for(j = 1; j <= n; j <<= 1){ 31 p = 0; 32 for(i = n - j; i < n; i++)y[p++] = i; 33 for(i = 0; i < n; i++)if(sa[i] >= j)y[p++] = sa[i] - j; 34 for(i = 0; i < m; i++)c[i] = 0; 35 for(i = 0; i < n; i++)c[x[y[i]]]++; 36 for(i = 1; i < m; i++)c[i] += c[i - 1]; 37 for(i = n - 1; i >= 0; i--)sa[--c[x[y[i]]]] = y[i]; 38 swap(x, y); 39 p = 1; 40 x[sa[0]] = 0; 41 for(i = 1; i < n; i++) 42 x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j] ? p - 1:p++; 43 if(p >= n)break; 44 m = p; 45 } 46 } 47 48 void get_height(int s[], int n) 49 { 50 int i, j, k = 0; 51 //cout<<"SA:"<
= t){ 69 num++; 70 if(num >= k)return true; 71 } 72 else num = 1; 73 } 74 return false; 75 } 76 77 int s[maxn]; 78 int main() 79 { 80 while(scanf("%d%d", &n, &k) != EOF){ 81 //scanf("%s", str); 82 int m = -inf; 83 for(int i = 0; i < n; i++){ 84 scanf("%d", &s[i]); 85 m = max(m, s[i]); 86 cnt[i] = 0; 87 } 88 s[n] = cnt[n] = 0; 89 build_sa(s, n + 1, m + 1); 90 //cout<<1<

 

转载于:https://www.cnblogs.com/wyboooo/p/9865584.html

你可能感兴趣的文章
dojo 学习日记 之 数组操作
查看>>
Linux基础初识(七)
查看>>
Android完全关闭应用程序
查看>>
页面加载时加Loading效果
查看>>
113.Path Sum II(append 和+的区别)
查看>>
字符串的操作String
查看>>
F#基础教程 简介
查看>>
java 反射: 当Timestamp类型的属性值为null时,设置默认值
查看>>
DOS命令
查看>>
爬取图片(一)
查看>>
playbook部署nginx
查看>>
一段有用的javascript加密解密
查看>>
[Source] 温柔的图片
查看>>
hexo+github搭建个人博客
查看>>
网络层
查看>>
3-----Docker实例-安装MySQL
查看>>
20172310 2017-2018-2 《程序设计与数据结构》第六周学习总结
查看>>
安卓学习第22课——seekBar
查看>>
iOS中的一些 第三方库 -
查看>>
2015/11/1用Python写游戏,pygame入门(1):pygame的安装
查看>>