后缀自动机

题目链接: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\n", 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值