码迷,mamicode.com
首页 > 编程语言 > 详细

hdu 3518 Boring counting 后缀数组 height分组

时间:2018-02-10 23:28:24      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:分组   printf   fine   重叠   long   长度   count   php   ==   

题目链接

题意

对于给定的字符串,求有多少个 不重叠的子串 出现次数 \(\geq 2\).

思路

枚举子串长度 \(len\),以此作为分界值来对 \(height\) 值进行划分。

显然,对于每一组,组内子串具有一个长度为 \(len\) 的公共前缀。

至于是否重叠,只需判断 \(sa_{max}-sa_{min}\geq len\).

对于组间,显然它们的公共前缀互不相同,所以答案即为\(\sum_{len}\sum_{group}\)

Code

#include <bits/stdc++.h>
#define maxn 1010
using namespace std;
typedef long long LL;
int wa[maxn], wb[maxn], wv[maxn], wt[maxn], h[maxn], rk[maxn], sa[maxn], n, r[maxn];
char s[maxn];
char s1[maxn], s2[maxn];
bool cmp(int* r, int a, int b, int l) { return r[a] == r[b] && r[a+l] == r[b+l]; }
void init(int* r, int* sa, int n, int m) {
    int* x=wa, *y=wb, *t, i, j, p;
    for (i = 0; i < m; ++i) wt[i] = 0;
    for (i = 0; i < n; ++i) ++wt[x[i] = r[i]];
    for (i = 1; i < m; ++i) wt[i] += wt[i - 1];
    for (i = n-1; i >= 0; --i) sa[--wt[x[i]]] = i;
    for (j = 1, p = 1; p < n; j <<= 1, m = p) {
        for (p = 0, i = n-j; i < n; ++i) y[p++] = i;
        for (i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
        for (i = 0; i < n; ++i) wv[i] = x[y[i]];
        for (i = 0; i < m; ++i) wt[i] = 0;
        for (i = 0; i < n; ++i) ++wt[wv[i]];
        for (i = 1; i < m; ++i) wt[i] += wt[i - 1];
        for (i = n-1; i >= 0; --i) sa[--wt[wv[i]]] = y[i];
        t = x, x = y, y = t, x[sa[0]] = 0;
        for (p = 1, i = 1; i < n; ++i) x[sa[i]] = cmp(y, sa[i], sa[i-1], j) ? p - 1 : p++;
    }

    for (i = 0; i < n; ++i) rk[sa[i]] = i;
    int k = 0;
    for (i = 0; i < n - 1; h[rk[i++]] = k) {
        for (k = k ? --k : 0, j = sa[rk[i] - 1]; r[i+k] == r[j+k]; ++k);
    }
}
void work() {
    int tot=0, m=0;
    for (int i = 0; s[i]; ++i) m = max(r[tot++] = s[i], m);
    r[tot++] = 0;
    init(r, sa, tot, ++m);
    int ans = 0;
    for (int len = 1; len < (tot+1)/2; ++len) {
        int minn = maxn, maxx = 0;
        for (int i = 1; i < tot; ++i) {
            if (h[i] < len) {
                ans += maxx-minn >= len ? 1 : 0;
                minn = sa[i], maxx = sa[i];
            }
            else minn = min(minn, sa[i]), maxx = max(maxx, sa[i]);
        }
        ans += maxx-minn >= len ? 1 : 0;
    }
    printf("%d\n", ans);
}
int main() {
    while (scanf("%s", s) != EOF && s[0] != '#') work();
    return 0;
}

hdu 3518 Boring counting 后缀数组 height分组

标签:分组   printf   fine   重叠   long   长度   count   php   ==   

原文地址:https://www.cnblogs.com/kkkkahlua/p/8439945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!