后缀数组 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], t2[N], c[N];
int a[N], A[N];

void da(int *s, int n, int m) {
    int i, p, *x = t, *y = t2;
    for (i=0; i<m; ++i) c[i] = 0;
    for (i=0; i<n; ++i) c[x[i]=s[i]]++;
    for (i=1; i<m; ++i) c[i] += c[i-1];
    for (i=n-1; i>=0; --i) sa[--c[x[i]]] = i;
    for (int k=1; k<=n; k<<=1) {
        for (p=0, i=n-k; i<n; ++i) y[p++] = i;
        for (i=0; i<n; ++i) if (sa[i] >= k) y[p++] = sa[i] - k;
        for (i=0; i<m; ++i) c[i] = 0;
        for (i=0; i<n; ++i) c[x[y[i]]]++;
        for (i=0; i<m; ++i) c[i] += c[i-1];
        for (i=n-1; i>=0; --i) sa[--c[x[y[i]]]] = y[i];
        std::swap (x, y);
        p = 1; x[sa[0]] = 0;
        for (i=1; i<n; ++i) {
            x[sa[i]] = (y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k] ? p - 1 : p++);
        }
        if (p >= n) break;
        m = p;
    }
}

void calc_height(int n) {
    int i, k = 0;
    for (i=0; i<n; ++i) rank[sa[i]] = i;
    for (i=0; i<n; ++i) {
        if (k) k--;
        int j = sa[rank[i]-1];
        while (a[i+k] == a[j+k]) k++;
        height[rank[i]] = k;
    }
}

int n, k;

bool check(int m) {
    int cnt = 0;
    for (int i=1; i<n; ++i) {
        if (height[i] >= m) {
            cnt++;
            //height[i] = LCP (suffix (sa[i-1], sa[i]));
            if (cnt + 1 >= k) {
                return true;
            }
        } else {
            cnt = 0;
        }
    }
    return false;
}

int main() {
    while (scanf ("%d%d", &n, &k) == 2) {
        for (int i=0; i<n; ++i) {
            scanf ("%d", a+i);
            A[i] = a[i];
        }
        std::sort (A, A+n);
        for (int i=0; i<n; ++i) {
            a[i] = std::lower_bound (A, A+n, a[i]) - A + 1;
        }
        a[n++] = 0;

        da (a, n, 20000);
        calc_height (n);

        int ans = 0;
        int left = 0, right = n - 1;
        while (left <= right) {
            int mid = left + right >> 1;
            if (check (mid)) {
                ans = std::max (ans, mid);
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        printf ("%d\n", ans);
    }
    return 0;
}

  

时间: 2024-10-28 14:45:42

后缀数组 POJ 3261 Milk Patterns的相关文章

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

[题目链接] 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次

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

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

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