题目地址:https://nanti.jisuanke.com/t/A2018

题目:


求字符串出现次数在[L,R]的子串数目

 

解题思路:


求出至少出现L次的子串数目ans1,和至少出现R+1次的子串数目ans2,最终结果为ans1-ans2。

for(int i = 1; i <= n - k + 1; i++)
{
  int l = i, r = i + k - 2;// [i + 1, i + k - 1]
  res += max(0, rmq(l, r) - height[i]);
}

至少出现次数为1特殊处理下,即字符串的所有子串数目。

 

ac代码:


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100020;
typedef long long ll;
//不能声明ws,保留字
int sa[maxn], wv[maxn], wss[maxn], wa[maxn], wb[maxn], r[maxn];
char s[maxn];
bool cmp(int *r, int a, int b, int l)
{
    return r[a] == r[b] && r[a + l] == r[b + l];
}
//O(nlogn)读入下标从0开始
void get_sa(int *r, int *sa, int n, int m)
{
    int *x=wa, *y=wb;
    int p =0, i, j;
    for(i = 0; i < m; i++) wss[i] = 0;
    for(i = 0; i < n; i++) wss[ x[i]=r[i] ]++;
    for(i = 1; i <= m; i++) wss[i] += wss[i - 1];
    for(i = n - 1; i >= 0; i--) sa[--wss[x[i]]] = i;
    for(j = 1, p = 1; p < n; j *= 2, m = p)
    {
        //对第二关键字排序
        for(p = 0, i = n - j; i < n; i++) // [n-j,n)没有内容
            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]]; //排名为i的第二关键字对应的第一关键字的排名,x此时相当于rankk,y相当于第二关键字的sa
        for(i = 0; i < m; i++) wss[i] = 0;
        for(i = 0; i < n; i++) wss[wv[i]]++;
        for(i = 1; i <= m; i++) wss[i] += wss[i - 1];
        for(i = n - 1; i >= 0; i--) sa[--wss[wv[i]]] = y[i];
        //相同的字符串排名相同
        swap(x,y);
        for(i = 1, p = 1, x[sa[0]] = 0; i < n; i++)
            x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p - 1 : p++;
    }
}
//O(n)
int rankk[maxn], height[maxn];
void get_height(int n)
{
    int k = 0;
    for(int i = 1; i <= n; i++) rankk[sa[i]] = i;
    for(int i = 0; i < n; i++)
    {
        k ? k-- : 0;//根据性质height[rank[i]] ≥ (height[rank[i-1]] -1)
        int j = sa[rankk[i] - 1];//上一名的开始下标
        while(r[i + k] == r[j + k]) k++;
        height[rankk[i]] = k;
    }
}
int a[maxn], d[maxn][20]; //2<<20长度
void rmq_init(int n)
{
    for(int i = 0; i < n; i++)
        d[i][0] = a[i];
    for(int j = 1; (1 << j) <= n; j++)
    {
        for(int i = 0; i + (1 << j) -1 < n; i++)
            d[i][j] = min(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]);
    }
}
int rmq(int l ,int r)
{
    int k = int(log((double)(r - l +1)) / log(2.0));
    return min(d[l][k], d[r - (1 << k) +1][k]);
}
ll solve(int k, int n)
{
    ll res = 0;
    if(k == 1)
    {
        res = (ll)n * (n + 1) / 2; //爆int!!
        for(int i = 1; i <= n; i++)
            res -= height[i];
    }
    else
    {
        for(int i = 1; i <= n - k + 1; i++)
        {
            int l = i, r = i + k - 2;// [i + 1, i + k - 1]
            res += max(0, rmq(l, r) - height[i]);
        }
    }
    return res;
}
int main()
{
    //freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
    int A, B;
    while(~scanf("%s", s))
    {
        scanf("%d %d", &A, &B);
        int n = strlen(s);
        for(int i = 0; i < n; i++)
            r[i] = (int)s[i];
        r[n] = 0;
        get_sa(r, sa, n + 1, 255);
        get_height(n);
        for(int i = 1; i <= n; i++)
            a[i - 1] = height[i];
        rmq_init(n);
        height[n + 1] = 0;
        ll ans1 = solve(A, n), ans2 = solve(B + 1, n);
        printf("%lld\n", ans1 - ans2);
    }
    return 0;
}