题目:

http://acm.hdu.edu.cn/showproblem.php?pid=6194

题意:

给定一个字符串,求恰好出现k次的子串有多少个

思路:

后缀数组处理出height数组,然后按顺序每次取一个长度为k的区间,这个区间的公共前缀设为L,即这个区间内的height数组的最小值,可以RMQ实现O(1)查询,那么意味着有L个子串出现了恰好k次,但是有可能这L个子串中有一些出现次数是大于k的,要减去,怎么减去呢?往两侧各扩展一个height值,用L减去两者中的最大值即可,当然k=1的时候记得特判

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 100000 + 10, INF = 0x3f3f3f3f;

int sa[N], height[N], rnk[N], wa[N], wb[N], c[N];
char str[N];
int s[N];
int dp[20][N];

bool cmp(int *r, int a, int b, int l)
{
    return r[a] == r[b] && r[a+l] == r[b+l];
}
void Rsort(int *x, int *y, int n, int m)
{
    for(int i = 0; i < m; i++) c[i] = 0;
    for(int i = 0; i < n; i++) c[x[y[i]]]++;
    for(int i = 1; i < m; i++) c[i] += c[i-1];
    for(int i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
}
void da(int *s, int n, int m)
{
    int *x = wa, *y = wb;
    for(int i = 0; i < n; i++) x[i] = s[i], y[i] = i;
    Rsort(x, y, n, m);
    for(int j = 1, p = 1; p < n; j *= 2, m = p)
    {
        p = 0;
        for(int i = n-j; i < n; i++) y[p++] = i;
        for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
        Rsort(x, y, n, m);
        swap(x, y); p = 1; x[sa[0]] = 0;
        for(int i = 1; i < n; i++) x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p-1 : p++;
    }
}
void get_height(int *s, int n)
{
    int i, j, k = 0;
    for(i = 0; i <= n; i++) rnk[sa[i]] = i;
    for(i = 0; i < n; height[rnk[i++]] = k)
        for(k ? --k : 0, j = sa[rnk[i]-1]; s[i+k] == s[j+k]; k++);
}
void ST(int *a, int n)
{
    for(int i = 1; i <= n; i++) dp[0][i] = a[i];
    for(int i = 1; (1<<i) <= n; i++)
        for(int j = 1; j <= n - (1<<i) + 1; j++)
            dp[i][j] = min(dp[i-1][j], dp[i-1][j+(1<<(i-1))]);
}
int RMQ(int l, int r)
{
    int k = log2(r - l + 1);
    return min(dp[k][l], dp[k][r-(1<<k)+1]);
}
int main()
{
    int t, k;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &k);
        scanf("%s", str);
        int len = strlen(str);
        for(int i = 0; i < len; i++) s[i] = str[i];
        s[len] = 0;
        da(s, len + 1, 150);
        get_height(s, len);
        height[len+1] = 0;
        ST(height, len);
        ll ans = 0;
        if(k == 1)
        {
            for(int i = 1; i <= len-k+1; i++)
                ans += max(len - sa[i] - max(height[i], height[i+1]), 0);
        }
        else
        {
            for(int i = 1; i <= len-k+1; i++)
                ans += max(RMQ(i+1, i+k-1) - max(height[i], height[i+k]), 0);
        }
        printf("%lld\n", ans);
    }
    return 0;
}