• 后缀自动机


    题目链接: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
    
  • 相关阅读:
    CycleGAN的原理及Pytorch实现
    Pix2Pix的原理及Pytorch实现
    DCGAN的原理及Pytorch实现
    Simple GAN的原理及Pytorch实现
    瑞数无限debugger完美处理
    Python selenium 设置 火狐 谷歌 无头模式
    TypeError: Cannot read property 'userAgent' of undefined at Timeout.task [as _onTimeout] (D:cnipa ode_modulesjsdomlibjsdomrowserWindow.js:516:19)
    fiddler 增加请求响应时间
    前端js对象转formData
    js 访问 URL 链接
  • 原文地址:https://www.cnblogs.com/lengsong/p/11331069.html
Copyright © 2020-2023  润新知