POJ3261(SummerTrainingDay10-G 后缀数组)

Milk Patterns

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

 
 //2017-08-11
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int N = ;
const int inf = 0x3f3f3f3f;
char str[N];
int n, r[N], k;
int wa[N], wb[N], wv[N], wss[N];
int Suffix[N];//Str下标为i ~ Len的连续子串(即后缀)
int SA[N];//满足Suffix[SA[1]] < Suffix[SA[2]] …… < Suffix[SA[Len]],即排名为i的后缀为Suffix[SA[i]](与Rank是互逆运算)
int Rank[N];//Suffix[i]在所有后缀中的排名
int Height[N];//height[i]表示Suffix[SA[i]]和Suffix[SA[i-1]]的最长公共前缀,也就是排名相邻的两个后缀的最长公共前缀
int H[N];//等于Height[Rank[i]],也就是后缀Suffix[i]和它前一名的后缀的最长公共前缀 //比较母串r中起始位置为a和b,长度都为len的子串是否相等
int cmp(int *r, int a, int b, int len)
{
return r[a]==r[b] && r[a+len]==r[b+len];
} //倍增算法求SA数组。
void da(int *r, int *SA, int n, int m)
{
int i, j, p, *x = wa, *y = wb, *t;
for(i = ; i < m; i++)wss[i] = ;
for(i = ; i < n; i++)wss[x[i]=r[i]]++;
for(i = ; i < m; i++)wss[i]+=wss[i-];
for(i = n-; i >= ; i--)SA[--wss[x[i]]]=i;
for(j = , p = ; p < n; j *= , m = p){
for(p = , i = n-j; i < n; i++)
y[p++] = i;
for(i = ; i < n; i++)
if(SA[i] >= j)
y[p++] = SA[i]-j;
for(i = ; i < n; i++)
wv[i] = x[y[i]];
for(i = ; i < m; i++)
wss[i] = ;
for(i = ; i < n; i++)
wss[wv[i]]++;
for(i = ; i < m; i++)
wss[i] += wss[i-];
for(i = n-; i >= ; i--)
SA[--wss[wv[i]]] = y[i];
for(t = x, x = y, y = t, p = , x[SA[]]=, i = ; i < n; i++)
x[SA[i]] = cmp(y, SA[i-], SA[i], j)?p-:p++;
}
} //计算height数组
void cal_Height(int *r, int *SA, int n)
{
int i, j, k = ;
for(i = ; i <= n; i++)Rank[SA[i]] = i;
for(i = ; i < n; Height[Rank[i++]] = k)
for(k?k--:, j=SA[Rank[i]-]; r[i+k]==r[j+k]; k++)
;
} int st[N][]; void init_rmq(int n)
{
for(int i=;i<=n;i++) st[i][]=Height[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
{
st[i][j]=min(st[i][j-],st[i+(<<(j-))][j-]);
}
} //询问后缀i和后缀j的最长公共前缀
int lcp(int i,int j)
{
i = Rank[i];
j = Rank[j];
if(i>j) swap(i,j);
i++;
int k=;
while(i+(<<(k+)) <= j) k++;
return min(st[i][k],st[j-(<<k)+][k]);
} bool check(int len){
int cnt = ;
for(int i = ; i <= n; i++){
if(Height[i] >= len){//将Height进行分组,每组内Height值均大于k。
cnt++;
}else cnt = ;//重新分组
if(cnt >= k)return true;//一组内元素个数不小于k,说明存在
}
return false;
} int main()
{
while(scanf("%d%d", &n, &k)!=EOF)
{
for(int i = ; i < n; i++)
scanf("%d", &r[i]);
da(r, SA, n+, );
cal_Height(r, SA, n);
//二分答案,进行判定
int l = , r = n, mid, ans = ;
while(l <= r){
mid = (l+r)/;
if(check(mid)){
ans = mid;
l = mid+;
}else r = mid-;
}
printf("%d\n", ans);
} return ;
}
上一篇:凹凸曼的修改zencart 程序(经典!)


下一篇:【背包型动态规划】灵魂分流药剂(soultap) 解题报告