POJ3261-Milk Patterns-后缀数组

可重叠重复k次的最长子串长度。

还是使用二分答案对heigh数组分组的做法。

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <vector>
 5 #include <iostream>
 6
 7 using namespace std;
 8 const int maxn = 2e4+10;
 9
10 int s[maxn];
11 int sa[maxn],rank[maxn],height[maxn];
12
13 int t[maxn],t2[maxn],c[maxn];
14 void build(int n,int m)
15 {
16     int i,j,p,*x = t, *y = t2;
17
18     for(i=0;i<m;i++)c[i]=0;
19     for(i=0;i<n;i++)c[x[i]=s[i]]++;
20     for(i=1;i<m;i++)c[i]+=c[i-1];
21     for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
22     for(j=1;j<=n;j<<=1)
23     {
24         p=0;
25         for(i=n-j;i<n;i++)y[p++]=i;
26         for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
27         for(i=0;i<m;i++)c[i]=0;
28         for(i=0;i<n;i++)c[x[y[i]]]++;
29         for(i=1;i<m;i++)c[i]+=c[i-1];
30         for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
31         swap(x,y);
32         p=1;x[sa[0]]=0;
33         for(i=1;i<n;i++)
34             x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;
35         if(p>=n)break;
36         m=p;
37     }
38     n--;
39     int k = 0;
40     for(int i=0;i<=n;i++) rank[sa[i] ] = i;
41     for(int i=0;i<n;i++)
42     {
43         if(k) k--;
44         int j = sa[rank[i]-1];
45         while(s[i+k]==s[j+k]) k++;
46         height[rank[i] ] = k;
47     }
48 }
49
50 int N,K;
51
52 bool check(int len,int k)
53 {
54     int cnt = 1;
55     for(int i=2;i<=N;i++)
56     {
57         if(height[i] >= len)
58         {
59             cnt++;
60             if(cnt >= k) return true;
61         }
62         else
63             cnt = 1;
64     }
65     return false;
66 }
67
68 int main()
69 {
70     while(~scanf("%d%d",&N,&K))
71     {
72         int Max = 0;
73         for(int i=0,tmp;i<N;i++)
74         {
75             scanf("%d",&s[i]);
76             Max = max(Max,s[i]);
77         }
78         s[N] = 0;
79         build(N+1,Max+1);
80
81         int l=0,r=N,mid;
82         int ans = 0;
83
84         while(l<=r)
85         {
86             mid = (l+r)>>1;
87             if(check(mid,K)){
88                 l = mid+1;
89                 ans = mid;
90             }
91             else
92                 r = mid-1;
93         }
94         printf("%d\n",ans);
95     }
96 }

POJ

时间: 2024-10-01 03:09:07

POJ3261-Milk Patterns-后缀数组的相关文章

[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 后缀数组+二分

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

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

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 将值 ≥ (当前二分长度) 的做一次贡

[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 <

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

poj3261 Milk Patterns(后缀数组)

Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 12571 Accepted: 5572 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

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