POJ 3261 后缀数组+二分

题意:

题目链接:http://poj.org/problem?id=3261
在一个字符串S中求至少出现k次的最长的字符串的长度。

思路:

经典题,后缀数组+二分
注意此题需要离散化。

代码:

#include 
#include 
#include 
using namespace std;
const int MAXN = 2e4 + 10;
const int INF = 0x3f3f3f3f;

int n, k;
int t1[MAXN], t2[MAXN], c[MAXN];

bool cmp(int *r, int a, int b, int l) {
    return r[a] == r[b] && r[a + l] == r[b + l];
}

void build(int a[],int sa[],int rk[],int height[],int n,int m) {
    n++;
    int i, j, p, *x = t1, *y = t2;
    //第一轮基数排序,如果s的最大值很大,可改为快速排序
    for(i = 0; i < m; i++) c[i] = 0;
    for(i = 0; i < n; i++) c[x[i] = a[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(j = 1; j <= n; j <<= 1) {
        p = 0;
        //直接利用sa数组排序第二关键字
        for(i = n-j; i < n; i++)y[p++] = i;//后面的j个数第二关键字为空的最小
        for(i = 0; i < n; i++)if(sa[i] >= j)y[p++] = sa[i] - j;
        //这样数组y保存的就是按照第二关键字排序的结果
        //基数排序第一关键字
        for(i = 0; i < m; i++) c[i] = 0;
        for(i = 0; i < n; i++) c[x[y[i]]]++;
        for(i = 1; i < m; i++) c[i] += c[i-1];
        for(i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
        //根据sa和x数组计算新的x数组
        swap(x,y);
        p = 1;
        x[sa[0]] = 0;
        for(i = 1; i < n; i++)
            x[sa[i]] = cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        if(p >= n)break;
        m = p;//下次基数排序的最大值
    }
    int k = 0;
    n--;
    for(i = 0; i <= n; i++)rk[sa[i]] = i;
    for(i = 0; i < n; i++)
    {
        if(k)   k--;
        j = sa[rk[i]-1];
        while(a[i+k] == a[j+k])
            k++;
        height[rk[i]] = k;
    }
}

int sa[MAXN], height[MAXN], rk[MAXN], a[MAXN];

bool check (int x) {
    int cnt = 1;
    for (int i = 1; i <= n; i++) {
        if (height[i] >= x) ++cnt;
        else {
            if (cnt >= k) return true;
            cnt = 1;
        }
    }
    return cnt >= k;
}

int solve (int l, int r) {
    int res = -1;
    while (l <= r) {
        int mid = (l + r) >> 1;
        if (check (mid)) {
            res = mid;
            l = mid + 1;
        }
        else r = mid - 1;
    }
    return res;
}

int discre() {
    vector <int> vec;
    for (int i = 0; i < n; i++)
        vec.push_back(a[i]);
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    int Max = 0;
    for (int i = 0; i < n; i++) {
        a[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin() + 1;
        Max = max(Max, a[i]);
    }
    return Max + 1;
}

int main(){
    //freopen("in.txt", "r", stdin);
    while (scanf ("%d%d", &n, &k) == 2) {
        for (int i = 0; i < n; i++)
            scanf ("%d", &a[i]);
        a[n] = 0;
        int m = discre();
        build(a, sa, rk, height, n, m + 1);
        int ans = solve (0, n);
        printf ("%d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(二分/三分,后缀数组)