码迷,mamicode.com
首页 > 其他好文 > 详细

后缀自动机

时间:2019-08-10 14:03:38      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:clu   emc   ast   clipboard   hover   mes   ber   const   icpc   

题目链接: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

后缀自动机

标签:clu   emc   ast   clipboard   hover   mes   ber   const   icpc   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!