后缀自动机

题目链接:http://icpc.upc.edu.cn/problem.php?cid=1828&pid=7

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
char str[maxn];
int hou[maxn *2][30],link[maxn *2], num[maxn *2];
ll End[maxn *2];
int stra[maxn], strb[maxn *2],Size, last, len;

void add(int c){
    int p = last,np = ++Size;
    last = np,End[np] = 1;
    num[np] = num[p] + 1;
    while(!hou[p][c] && p)
        hou[p][c] = np,p = link[p];
    if(p == 0)
        link[np] = 1;
    else{
        int q = hou[p][c];
        if(num[p] + 1 == num[q])
            link[np] = q;
        else{
            int temp = ++Size;
            memcpy(hou[temp], hou[q], sizeof(hou[q]));
            num[temp] = num[p] + 1;
            link[temp] = link[q];
            link[q] = link[np] = temp;
            while(hou[p][c] == q && p)
                hou[p][c] = temp, p = link[p];
        }
    }
}
void build(){
    memset(hou, 0, sizeof(hou));
    memset(End, 0, sizeof(End));
    memset(stra, 0, sizeof(stra));
    memset(strb, 0, sizeof(strb));
    Size = last = 1;
    for(register int i = 0; i < len; ++i)
        add(str[i] - 'A');
    for(register int i = 1; i <= Size; ++i)
        stra[num[i]]++;
    for(register int i = 1; i <= len; ++i)
        stra[i] += stra[i - 1];
    for(register int i = 1; i <= Size; ++i)
        strb[stra[num[i]]--] = i;
    for(register int i = Size; i > 1; --i){
        int e = strb[i];
        End[link[e]] += End[e];
    }
}
void solve(){
    int A, B;
    scanf("%d %d", &A, &B);
    len = strlen(str);
    build();
    ll ans = 0;
    for(register int i = 1; i <= Size; ++i)
        if(End[i] >= A && End[i] <= B)
            ans += num[i] - num[link[i]];
    printf("%lld
", ans);
}
int main(){
    while(~scanf("%s", str))
        solve();
    return 0;
}

  

 

题目描述

Now you have a string consists of uppercase letters, two integers A and B. We call a substring wonderful substring when the times it appears in that string is between A and B (A ≤ times ≤ B). Can you calculate the number of wonderful substrings in that string?

输入

Input has multiple test cases.
For each line, there is a string S, two integers A and B.
∑Length(S)≤2×10^6,1≤A≤B≤length(S) 

输出

For each test case, print the number of the wonderful substrings in a line.

样例输入

AAA 2 3
ABAB 2 2

样例输出

2
3
原文地址:https://www.cnblogs.com/lengsong/p/11331069.html