poj3261: Milk Patterns 产奶的模式

poj3261 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 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.

农夫John发现他的奶牛产奶的质量一直在变动。经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠。我们称之为一个“模式”。 John的牛奶按质量可以被赋予一个0到1000000之间的数。并且John记录了N(1<=N<=20000)天的牛奶质量值。他想知道最长的出现了至少K(2<=K<=N)次的模式的长度。比如1 2 3 2 3 2 3 1 中 2 3 2 3出现了两次。当K=2时,这个长度为4。

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.

Line 1: 两个整数 N,K。
Lines 2..N+1: 每行一个整数表示当天的质量值。

Output

Line 1: One integer, the length of the longest pattern which occurs at least K times

Line 1: 一个整数:N天中最长的出现了至少K次的模式的长度

Sample Input

8 2
1
2
3
2
3
2
3
1

Sample Output

4

题目大意

给定一个字符串,求至少出现k次的最长重复子串,这k个子串可以重叠。

题解

解法参见论文例4。简单来说就是二分答案,判断有没有连续k-1以上个height值大于等于m,若有则存在长度为m的k个相同的子串,否则不存在。复杂度 O(nlogn)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 20000 + 10, A = 1000000 + 10;

int n, K, a[N];
int ans;
int sa[2][N], rank[2][N], v[N], height[N], p, q, k;
int dt[N], hs[N], tot;

inline void in(int &x){
    x = 0; char ch = getchar();
    while(ch < '0' || ch > '9') ch = getchar();
    while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = getchar();}
}

void init(){
    scanf("%d%d", &n, &K);
    for(int i = 1; i <= n; i++){
        in(a[i]); dt[i] = a[i];
    }
    sort(dt+1, dt+n+1);
    hs[++tot] = dt[1];
    for(int i = 2; i <= n; i++)
        if(dt[i] != dt[i-1]) hs[++tot] = dt[i];
    for(int i = 1; i <= n; i++) a[i] = lower_bound(hs, hs+tot+1, a[i]) - hs;
}
void calsa(int *sa1, int *rk1, int *sa2, int *rk2){
    for(int i = 1; i <= n; i++) v[rk1[sa1[i]]] = i;
    for(int i = n; i >= 1; i--) if(sa1[i] > k) sa2[v[rk1[sa1[i]-k]]--] = sa1[i] - k;
    for(int i = n-k+1; i <= n; i++) sa2[v[rk1[i]]--] = i;
    for(int i = 1; i <= n; i++)
        rk2[sa2[i]] = rk2[sa2[i-1]] + (rk1[sa2[i]] != rk1[sa2[i-1]] || rk1[sa2[i]+k] != rk1[sa2[i-1]+k]);
}
void calheight(){
    int k = 0;
    for(int i = 1; i <= n; i++){
        if(k) k--;
        int j = sa[p][rank[p][i]-1];
        while(a[i+k] == a[j+k]) k++;
        height[rank[p][i]] = k;
    }
}
bool check(int m){
    int tmp = 0;
    for(int i = 1; i <= n; i++)
        if(height[i] >= m){
            tmp++;
            if(tmp == K-1) return true;
        }
        else tmp = 0;
    return false;
}
void work(){
    p = 0, q = 1;
    for(int i = 1; i <= n; i++) v[a[i]]++;
    for(int i = 1; i <= n; i++) v[i] += v[i-1];
    for(int i = 1; i <= n; i++) sa[p][v[a[i]]--] = i;
    for(int i = 1; i <= n; i++) rank[p][sa[p][i]] = rank[p][sa[p][i-1]] + (a[sa[p][i-1]] != a[sa[p][i]]);
    k = 1;
    while(k < n){
        calsa(sa[p], rank[p], sa[q], rank[q]);
        p ^= 1, q ^= 1, k <<= 1;
    }
    calheight();
    int l = 1, r = n, m;
    while(l <= r){
        m = l+r>>1;
        if(check(m)) ans = m, l = m + 1;
        else r = m - 1;
    }
    printf("%d\n", ans);
}
int main(){
    init();
    work();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值