poj_3261_Milk Patterns(后缀数组)

题目链接:poj_3261_Milk Patterns

题意:

给你一串数,让你找重复出现不少于k次的子串的最长长度,重复出现可重叠

题解:

老套路,还是二分答案,然后用height数组来check答案

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define F(i,a,b) for(int i=a;i<=b;i++)
 4 using namespace std;
 5
 6 namespace suffixarray{
 7     #define FN(n) for(int i=0;i<n;i++)
 8     const int N =2E5+7;
 9     int rnk[N],sa[N],height[N],c[N],s[N];
10     void getsa(int n,int m,int *x=rnk,int *y=height){
11         FN(m)c[i]=0;FN(n)c[x[i]=s[i]]++;FN(m)c[i+1]+=c[i];
12         for(int i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
13         for(int k=1,p;p=0,k<=n;k=p>=n?N:k<<1,m=p){
14             for(int i=n-k;i<n;i++)y[p++]=i;
15             FN(n)if(sa[i]>=k)y[p++]=sa[i]-k;
16             FN(m)c[i]=0;FN(n)c[x[y[i]]]++;FN(m)c[i+1]+=c[i];
17             for(int i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
18             swap(x,y),p=1,x[sa[0]]=0;
19             for(int i=1;i<n;i++)
20             x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
21         }
22         FN(n)rnk[sa[i]]=i;
23         for(int i=0,j,k=0;i<n-1;height[rnk[i++]]=k)
24         for(k=k?k-1:k,j=sa[rnk[i]-1];s[i+k]==s[j+k];k++);
25     }
26 }
27 using namespace suffixarray;
28 int n,k,a[N],ed;
29
30 inline int getid(int x){return lower_bound(a+1,a+1+ed,x)-a;}
31
32 bool check(int x,int cnt=1)
33 {
34     F(i,2,n)if(height[i]>=x){if(++cnt>=k)return 1;}else cnt=1;
35     return 0;
36 }
37
38 int main()
39 {
40     while(~scanf("%d%d",&n,&k))
41     {
42         F(i,1,n)scanf("%d",a+i),s[i-1]=a[i];
43         sort(a+1,a+1+n),ed=1;
44         F(i,2,n)if(a[i]!=a[ed])a[++ed]=a[i];
45         F(i,0,n-1)s[i]=getid(s[i]);
46         s[n]=0,getsa(n+1,ed+1);
47         int l=1,r=n,mid,ans=0;
48         while(l<=r)mid=(l+r)>>1,check(mid)?l=(ans=mid)+1:r=mid-1;
49         printf("%d\n",ans);
50     }
51     return 0;
52 }

时间: 2024-10-21 05:42:57

poj_3261_Milk Patterns(后缀数组)的相关文章

poj3261Milk Patterns 后缀数组

题目地址:http://poj.org/problem?id=3261 题目: 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

poj 3261 Milk Patterns 后缀数组+二分

1 /*********************************************************** 2 题目: Milk Patterns(poj 3261) 3 链接: http://poj.org/problem?id=3261 4 题意: 给一串数字,求这些数字中公共子串个数大于k的 5 最长串. 6 算法: 后缀数组+二分 7 ***********************************************************/ 8 #incl

[POJ3261] Milk Patterns (后缀数组+二分)

题目概述: 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 t

POJ 3261 Milk Patterns 后缀数组

用后缀数组求重复出现至少k次的可重叠最长子串的长度, 当然是可以用hash搞的,用后缀数组的话,只要在分组之后看看个数是不是大于等于k #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <map> #include <set> #include <climits>

POJ 3261 Milk Patterns ( 后缀数组 &amp;&amp; 出现k次最长可重叠子串长度 )

题意 : 给出一个长度为 N 的序列,再给出一个 K 要求求出出现了至少 K 次的最长可重叠子串的长度 分析 : 后缀数组套路题,思路是二分长度再对于每一个长度进行判断,判断过程就是对于 Height 数组进行限定长度的分组策略,如果有哪一组的个数 ≥  k 则说明可行! 分组要考虑到一个事实,对于每一个后缀,与其相匹配能够产生最长的LCP长度的串肯定是在后缀数组中排名与其相邻. 一开始对分组的理解有误,所以想了一个错误做法 ==> 遍历一下 Height 将值 ≥ (当前二分长度) 的做一次贡

POJ - 3261 Milk Patterns (后缀数组求可重叠的 k 次最长重复子串)

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 pattern

POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次

Milk Patterns 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 r

[Poj3261] [Bzoj1717] [后缀数组论文例题,USACO 2006 December Gold] Milk Patterns [后缀数组可重叠的k次最长重复子串]

和上一题(POJ1743,上一篇博客)相似,只是二分的判断条件是:是否存在一段后缀的个数不小于k 1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cmath> 7 #include <ctime> 8 #include <map&

【POJ3261】Milk Patterns 后缀数组

水题不好意思说题解. 说说题意吧: 给一个字符串(数字串),然后求最长k次重复子串. 即某串在字符串中重复了至少k次,求这种串的最长长度. 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 21000 using namespace std; struct LSH { int x,id; bool operator <