POJ 3261 Milk Patterns sa+二分

                    Milk Patterns

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 11944   Accepted: 5302
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 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

Source

USACO 2006 December Gold

题意:

给定一个"字符串",长度N<=20000

输入k

求满足出现k次或以上这个条件的所有子串中(可以重复),最长的子串的长度。

后缀数组+二分

复杂度:O(nlogn)

求出height数组后,由于最长子串的长度的范围为1~N

则二分长度

对于每一长度mid,对height数组进行分组

使得每一组内要不只有一个元素,要不该组内的height的值都>=mid,即该组内的LCP的长度>=mid

若存在一个组,该组内的元素个数>=k

说明答案至少为mid

继续二分mid~right

否则继续二分left~mid

47ms

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4
  5 using namespace std;
  6
  7 const int MAXN=20000+5;
  8 const int MAXM=1000000+5;
  9
 10 int c[MAXM];
 11 int t1[MAXN];
 12 int t2[MAXN];
 13 int sa[MAXN];
 14 int Rank[MAXN];
 15 int height[MAXN];
 16 int s[MAXN];
 17
 18 void build_sa(int N,int m)
 19 {
 20     int i,*x=t1,*y=t2;
 21     for(i=0;i<m;i++)
 22         c[i]=0;
 23     for(i=0;i<N;i++)
 24         c[x[i]=s[i]]++;
 25     for(i=1;i<m;i++)
 26         c[i]+=c[i-1];
 27     for(i=N-1;i>=0;i--)
 28         sa[--c[x[i]]]=i;
 29
 30     for(int k=1;k<=N;k<<=1)
 31     {
 32         int p=0;
 33         for(i=N-k;i<N;i++)
 34             y[p++]=i;
 35         for(i=0;i<N;i++)
 36             if(sa[i]>=k)
 37                 y[p++]=sa[i]-k;
 38
 39         for(i=0;i<m;i++)
 40             c[i]=0;
 41         for(i=0;i<N;i++)
 42             c[x[y[i]]]++;
 43         for(i=1;i<m;i++)
 44             c[i]+=c[i-1];
 45         for(i=N-1;i>=0;i--)
 46             sa[--c[x[y[i]]]]=y[i];
 47
 48         swap(x,y);
 49
 50         p=1;
 51         x[sa[0]]=0;
 52         for(i=1;i<N;i++)
 53         {
 54             x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
 55         }
 56         if(p>=N)
 57             break;
 58         m=p;
 59     }
 60 }
 61
 62 void getHeight(int N)
 63 {
 64     int i,k=0;
 65     for(i=1;i<=N;i++)
 66         Rank[sa[i]]=i;
 67     for(i=0;i<N;i++)
 68     {
 69         if(k)
 70             k--;
 71         int j=sa[Rank[i]-1];
 72         while(s[i+k]==s[j+k])
 73             k++;
 74         height[Rank[i]]=k;
 75     }
 76 }
 77
 78 bool valid(int len,int N,int K)
 79 {
 80     int i=1;
 81     while(true)
 82     {
 83         while(i<=N&&height[i]<len)
 84             i++;
 85         if(i>N)
 86             return false;
 87         int cnt=0;
 88         while(i<=N&&height[i]>=len)
 89         {
 90             i++;
 91             cnt++;
 92         }
 93         if(cnt+1>=K)
 94             return true;
 95     }
 96 }
 97
 98 int solve(int N,int K)
 99 {
100     int lb=1;
101     int rb=N;
102     while(rb-lb>1)
103     {
104         int mid=(rb+lb)>>1;
105         if(valid(mid,N,K))
106             lb=mid;
107         else
108             rb=mid;
109     }
110     return lb;
111 }
112 int main()
113 {
114     int N,K;
115     while(~scanf("%d%d",&N,&K))
116     {
117         int m=0;
118         for(int i=0;i<N;i++)
119         {
120             scanf("%d",&s[i]);
121             s[i]++;
122             if(s[i]>m)
123                 m=s[i];
124         }
125         m++;
126         s[N]=0;
127
128         build_sa(N+1,m);
129         getHeight(N);
130
131         int ans=solve(N,K);
132
133         printf("%d\n",ans);
134
135         /*
136         for(int i=0;i<N+1;i++)
137             printf("%d ",sa[i]);
138         printf("\n");
139         for(int i=0;i<N;i++)
140             printf("%d ",Rank[i]);
141         printf("\n");
142         for(int i=0;i<N;i++)
143             printf("%d ",height[i]);
144         */
145     }
146     return 0;
147 }

时间: 2024-12-21 12:56:12

POJ 3261 Milk Patterns sa+二分的相关文章

POJ 3261 Milk Patterns (求可重叠的k次最长重复子串)

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

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 PatternsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 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 c

POJ 3261 Milk Patterns

Milk Patterns Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 326164-bit integer IO format: %lld      Java class name: Main Farmer John has noticed that the quality of milk given by his cows varies from day t

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(后缀数组+二分答案)

[题目链接] http://poj.org/problem?id=3261 [题目大意] 求最长可允许重叠的出现次数不小于k的子串. [题解] 对原串做一遍后缀数组,二分子串长度x,将前缀相同长度超过x的后缀分组, 如果存在一个大小不小于k的分组,则说明答案可行,分治得到最大可行解就是答案. [代码] #include <cstdio> #include <cstring> #include <vector> using namespace std; const int

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 次最长重复子串.给定一个字符串,求至少出现 k 次的最长重复子串,这 k 个子串可以重叠. 分析:与POJ 1743做法类似,先二分答案,height数组分段后统计 LCP>=m 的子串的个数. #include <cstdio> #include <cstring> #include <algorithm> const int N = 2e4 + 5; int sa[N], rank[N], height[N]; int t[N],

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

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