POJ 3261 Milk Patterns

题意:给出一个序列,求出重复次数大于等于K次的最长子串的长度。

思路:利用后缀数组的height数组。我们可以二分长度,利用长度对height进行分组,连续的且height值不小于长度的后缀为一组。因为一组内的height值都大于等于长度,且组内后缀的个数大于等于K,那么以最小height值的子串就会出现K次。否则,就不满足要求,继续二分。复杂度为nlgn。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

template<class T>
inline bool read(T &n){
    T x = 0, tmp = 1; char c = getchar();
    while ((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if (c == EOF) return false;
    if (c == '-') c = getchar(), tmp = -1;
    while (c >= '0' && c <= '9') x *= 10, x += (c - '0'), c = getchar();
    n = x*tmp;
    return true;
}

template <class T>
inline void write(T n) {
    if (n < 0) {
        putchar('-');
        n = -n;
    }
    int len = 0, data[20];
    while (n) {
        data[len++] = n % 10;
        n /= 10;
    }
    if (!len) data[len++] = 0;
    while (len--) putchar(data[len] + 48);
}

const int MAX =30000;

int N,K;
int a[MAX];
vector<int> b;
int sa[MAX];
int str[MAX];
int height[MAX];

void radix(int * str, int *a, int *b, int n, int m){
    static int count[MAX];
    memset(count,0,sizeof(count));
    for(int i = 0; i < n; ++i) ++count[str[a[i]]];
    for(int i = 1; i <= m; ++i) count[i] += count[i-1];
    for(int i = n -1; i >= 0; --i) b[--count[str[a[i]]]] = a[i];
}

void suffix_array(int* str,int * sa, int n, int m)
{
    static int rank[MAX],a[MAX],b[MAX];
    for(int i = 0; i < n; ++i) rank[i] =i;
    radix(str,rank,sa,n,m);

    rank[sa[0]] = 0;
    for(int i = 1; i < n; ++i)
        rank[sa[i]]= rank[sa[i-1]] +(str[sa[i]]!=str[sa[i-1]]);
    for(int i = 0; 1<<i< n; ++i){
        for(int j = 0; j < n; ++j){
            a[j] = rank[j]+1;
            b[j] = j + (1<<i) >=n? 0: rank[j + (1<<i)] + 1;
            sa[j] = j;
        }
        radix(b,sa,rank,n,n);
        radix(a,rank,sa,n,n);
        rank[sa[0]] = 0;
        for(int j = 1; j < n; ++j){
            rank[sa[j]] = rank[sa[j-1]] + (a[sa[j-1]] != a[sa[j]] || b[sa[j-1]] != b[sa[j]]);
        }
    }
}

void calc_height(int * str, int * sa, int * height, int n)
{
    static int rank[MAX];
    for(int i = 0 ; i < n; ++i) rank[sa[i]] = i;

    int h = 0;
    for(int i = 0; i < n; ++i){
        h = h == 0?0: h - 1;
        if(rank[i]!= 0)
            while(str[i + h] == str[sa[rank[i]-1] + h]) h++;
        height[rank[i]] = h;
    }
}

bool judge(int mid)
{
    int times = 1;
    for(int i = 0; i < N;++i){
        if(height[i] >= mid) times++;
        else times = 1;
        if(times >= K) return true;
    }
    return false;
}


int main(void)
{
    //freopen("input.txt","r",stdin);
    scanf("%d %d", &N,&K);
    for(int i = 0 ; i < N; ++i)
        scanf("%d",&a[i]),b.push_back(a[i]);

    sort(b.begin(),b.end());
    b.erase(unique(b.begin(),b.end()),b.end());
    for(int i = 0 ; i < N; ++i)
        str[i] = lower_bound(b.begin(),b.end(),a[i]) - b.begin() + 1;

    suffix_array(str,sa,N,20000);
    calc_height(str,sa,height,N);

    int lb = 1, ub = N;
    while(lb + 1 < ub){
        int mid = (lb + ub) >> 1;
        if(judge(mid)) lb = mid;
        else ub = mid;
    }

    printf("%d\n",lb);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值