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 ≤ KN) 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
  1 /*
2 题意:
3 给你一个长度为n的序列,你要找到重复相同至少K(2≤K≤N)次的样本的最长模式。输出最长是多少(这k次样本可以重叠一部分)
4
5 题解:
6 这道题原来我是想这先用后缀数组求出来height数组之后,然后只需要在height[i...n]这个范围内找到范围为m,使这个范围内的
7 最小值尽可能大就行
8 相当于滑动窗口,但是这个窗口的宽度是可变的(根据题目输入)
9 然后我就发现了这个问题总是在询问一个范围内的最小值,感觉可用线段树。虽然每次询问耗时使log(n),但是要询问好多次,总复杂度
10 相当于nlog(n),感觉还不行
11
12 百度一发,可以用二分枚举最后答案,然后判断一下这个答案可以不就可以了
13 怎么判断?
14 你只需要找出来height数组中连着的m-1个height值都大于你二分枚举出来那个值就行
15
16 */
17 #include <cstdlib>
18 #include <cstring>
19 #include <cstdio>
20 #include <algorithm>
21 using namespace std;
22
23 const int N = 200000+9;
24 int x[N], y[N], c[N];
25 int rank[N], height[N];
26 int sa[N],s[N],n,k;
27 bool pan(int *x,int i,int j,int k,int n)
28 {
29 int ti=i+k<n?x[i+k]:-1;
30 int tj=j+k<n?x[j+k]:-1;
31 return x[i]==x[j]&&ti==tj;
32 }
33 void build_SA(int n,int r)
34 {
35 int *x=rank,*y=height;
36 for(int i=0; i<r; i++)c[i]=0;
37 for(int i=0; i<n; i++)c[s[i]]++;
38 for(int i=1; i<r; i++)c[i]+=c[i-1];
39 for(int i=n-1; i>=0; i--)sa[--c[s[i]]]=i;
40 r=1;
41 x[sa[0]]=0;
42 for(int i=1; i<n; i++)
43 x[sa[i]]=s[sa[i]]==s[sa[i-1]]?r-1:r++;
44 for(int k=1; r<n; k<<=1)
45 {
46 int yn=0;
47 for(int i=n-k; i<n; i++)y[yn++]=i;
48 for(int i=0; i<n; i++)
49 if(sa[i]>=k)y[yn++]=sa[i]-k;
50 for(int i=0; i<r; i++)c[i]=0;
51 for(int i=0; i<n; i++)++c[x[y[i]]];
52 for(int i=1; i<r; i++)c[i]+=c[i-1];
53 for(int i=n-1; i>=0; i--)sa[--c[x[y[i]]]]=y[i];
54 swap(x,y);
55 r=1;
56 x[sa[0]]=0;
57 for(int i=1; i<n; i++)
58 x[sa[i]]=pan(y,sa[i],sa[i-1],k,n)?r-1:r++;
59 }
60 for(int i=0; i<n; i++)rank[i]=x[i];
61 }
62 void get_height(int n)
63 {
64 int i,j,k=0;
65 for(i=1; i<=n; i++)rank[sa[i]]=i;
66 for(i=0; i<n; i++)
67 {
68 if(k)k--;
69 else k=0;
70 j=sa[rank[i]-1];
71 while(s[i+k]==s[j+k])k++;
72 height[rank[i]]=k;
73 }
74 }
75 int check(int len)
76 {
77 int i=2,cnt=0;
78 while(1)
79 {
80 while(i<=n && height[i]>=len)
81 cnt++,i++;
82 if(cnt+1>=k)return 1;
83 if(i>=n)return 0;
84 while(i <=n &&height[i]<len)
85 i++;
86 cnt=0;
87 }
88 }
89
90 int main()
91 {
92 scanf("%d%d",&n,&k);
93 for(int i=0; i<n; i++)
94 scanf("%d",&s[i]),s[i]++;
95 s[n]=0;
96
97 build_SA(n+1,N);
98 get_height(n);
99 int l=1,r=n,ans=1,mid;
100 while(l<=r)
101 {
102 mid=(l+r)/2;
103 if(check(mid))
104 l=mid+1,ans=mid;
105 else
106 r=mid-1;
107 }
108 printf("%d\n",ans);
109 return 0;
110 }

Milk Patterns POJ - 3261 后缀数组的更多相关文章

  1. Milk Patterns POJ - 3261(后缀数组+二分)

    题意: 求可重叠的最长重复子串,但有一个限制条件..要至少重复k次 解析: 二分枚举k,对于连续的height 如果height[i] >= k 说明它们至少有k个元素是重复的,所以判断一下就好 ...

  2. POJ 3261 后缀数组

    题目链接:http://poj.org/problem?id=3261 题意:约翰注意到奶牛产奶的之类是不断变化的,虽然他不能预测从当天到下一天的变化情况但是他知道变化是有规律的,牛奶的质量由一个整数 ...

  3. poj 3261 后缀数组 找反复出现k次的子串(子串能够重叠)

    题目:http://poj.org/problem?id=3261 仍然是后缀数组的典型应用----后缀数组+lcp+二分 做的蛮顺的,1A 可是大部分时间是在调试代码.由于模板的全局变量用混了,而自 ...

  4. poj 3261 后缀数组 可重叠的 k 次最长重复子串

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16430   Accepted: 7252 Ca ...

  5. Milk Patterns - poj 3261 (求重复k次的最长子串)

    题目大意:给你一个数组,求这个数组里面至少重复k次的子串.   分析:后缀数组的练手题目...不过给的数字比较大,可以先离散化处理一下即可.   代码如下: ===================== ...

  6. Milk Patterns poj3261(后缀数组)

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9274   Accepted: 4173 Cas ...

  7. POJ 3261 (后缀数组 二分) Milk Patterns

    这道题和UVa 12206一样,求至少重复出现k次的最长字串. 首先还是二分最长字串的长度len,然后以len为边界对height数组分段,如果有一段包含超过k个后缀则符合要求. #include & ...

  8. [poj 3261]后缀数组+滑窗最小值

    题目链接:http://poj.org/problem?id=3261 这个是可以交叉的重复串,所以用height就可以了,但是题目说让重复k次以上,也就是直接做一个k-1长度的滑窗最小值,从这些最小 ...

  9. POJ 3261 后缀数组+二分

    思路: 论文题- 二分+对后缀分组 这块一开始不用基数排序 会更快的(其实区别不大) //By SiriusRen #include <cstdio> #include <cstri ...

随机推荐

  1. awk中的if ,else

    local pct="$(awk -v one="$1" -v two="$2" 'BEGIN{ if (two > 0) { printf & ...

  2. --safe-user-create

    此参数如果启用,用户将不能用grant语句创建新用户,除非用户有mysql数据库中user表的insert权限, ./mysqld_safe  --safe-user-create & 用-- ...

  3. 修改机器的hostname

    vi /etc/sysconfig/network hostname=你想设置的主机名 不重启器的情况下使显示名称变成 hostname  主机名

  4. 【Python】在CentOS6.8中安装pip9.0.1和setuptools33.1

    wget https://bootstrap.pypa.io/ez_setup.py python ez_setup.py install --如果这个文件安装需要下载的文件无法下载的话,手动下载,放 ...

  5. SSRF - Pikachu

    概述: SSRF(Server-Side Request Forgery:服务器端请求伪造) 其形成的原因大都是由于服务端提供了从其他服务器应用获取数据的功能,但又没有对目标地址做严格过滤与限制 导致 ...

  6. Py-上下文管理方法,描述符的应用,错误与异常

    上下文管理方法: 可以在exit里面弄一些内存清理的功能 class Open: def __init__(self,name): self.name=name def __enter__(self) ...

  7. Py编程方法,尾递归优化,map函数,filter函数,reduce函数

    函数式编程 1.面向过程 把大的问题分解成流程,按照流程来编写过程 2.面向函数 面向函数编程=编程语言定义的函数+数学意义上的函数先弄出数学意义上的方程式,再用编程方法编写这个数学方程式注意面向函数 ...

  8. Kafka分区分配策略(Partition Assignment Strategy)

    众所周知,Apache Kafka是基于生产者和消费者模型作为开源的分布式发布订阅消息系统(当然,目前Kafka定位于an open-source distributed event streamin ...

  9. 转 jmeter录制https请求

    jmeter录制https请求  文章转自:https://www.cnblogs.com/zhengna/p/10180998.html 工具:Jmeter4.0 + Java1.8 需求:对某ht ...

  10. 览器全面禁用三方 Cookie 全栈前端精选 4月16日

    览器全面禁用三方 Cookie 全栈前端精选 4月16日