ACM-ICPC 2018 焦作赛区网络预赛 H题 String and Times(SAM)

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

Input

Input has multiple test cases.

For each line, there is a string SS, two integers AA and BB.

\sum length(S) \le 2 \times 10^6∑length(S)≤2×106,

1 \le A \le B \le length(S)1≤A≤B≤length(S)

Output

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

样例输入复制

AAA 2 3
ABAB 2 2

样例输出复制

2
3

题目来源

ACM-ICPC 2018 焦作赛区网络预赛

题解:SAM模板题

参考代码:

//H  求子串出现次数在k1=<num<=k2;
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5+10;
char ss[200005];
const int LetterSize = 26;

int tot, last,ch[MAXN][LetterSize],fa[MAXN],len[MAXN];
int sum[MAXN],tp[MAXN],cnt[MAXN]; 

void init()
{
    last = tot = 1;
    len[1] = 0;
    memset(ch,0,sizeof ch);
    memset(fa,0,sizeof fa);
    memset(cnt,0,sizeof cnt);
}

void add( int x)
{
    int p = last, np = last = ++tot;
    len[np] = len[p] + 1, cnt[last] = 1;
    while( p && !ch[p][x]) ch[p][x] = np, p = fa[p];
    if(p == 0) fa[np] = 1;
    else
    {
        int q = ch[p][x];
        if( len[q] == len[p] + 1)
            fa[np] = q;
        else
        {
            int nq = ++tot;
            memcpy( ch[nq], ch[q], sizeof ch[q]);
            len[nq] = len[p] + 1, fa[nq] = fa[q], fa[q] = fa[np] = nq;
            while( p && ch[p][x] == q)  ch[p][x] = nq, p = fa[p];
        }
    }
}

void toposort()
{
    for(int i = 1; i <= len[last]; i++)   sum[i] = 0;
    for(int i = 1; i <= tot; i++)   sum[len[i]]++;
    for(int i = 1; i <= len[last]; i++)   sum[i] += sum[i-1];
    for(int i = 1; i <= tot; i++)   tp[sum[len[i]]--] = i;
}

int main()
{

    int k1,k2;
	while(scanf("%s",ss)!=EOF)
    {
    	init();
    	scanf("%d%d",&k1,&k2);
	    long long ans=0;
        for(int i=0,len=strlen(ss);i<len;i++) add(ss[i]-‘A‘);
        toposort();
        for(int i=tot;i;i--)
        {
            int p=tp[i],fp=fa[p];
            cnt[fp]+=cnt[p];
            if(cnt[p]>=k1 && cnt[p]<=k2) ans+=len[p]-len[fp];
        }
        printf("%lld\n",ans);
    }

    return 0;
}

  

原文地址:https://www.cnblogs.com/songorz/p/9651909.html

时间: 2024-08-30 10:30:41

ACM-ICPC 2018 焦作赛区网络预赛 H题 String and Times(SAM)的相关文章

ACM-ICPC 2018 焦作赛区网络预赛 B题 Mathematical Curse

A prince of the Science Continent was imprisoned in a castle because of his contempt for mathematics when he was young, and was entangled in some mathematical curses. He studied hard until he reached adulthood and decided to use his knowledge to esca

ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 33 

ACM-ICPC 2018 焦作赛区网络预赛 K题 Transport Ship

There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry the weight of V[i]V[i] and the number of the i^{th}ith kind of ship is 2^{C[i]} - 12C[i]?1. How many different schemes there are if you want to use thes

ACM-ICPC 2018 焦作赛区网络预赛 H、L

https://nanti.jisuanke.com/t/31721 题意 n个位置 有几个限制相邻的三个怎么怎么样,直接从3开始 矩阵快速幂进行递推就可以了 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll mod=1e9+7,maxn=9; 5 struct Matrix 6 { 7 ll m[maxn][maxn]; 8 Matrix() 9 { 10 memset(m

ACM-ICPC 2018 焦作赛区网络预赛 I题(滑稽)

不解释,看代码 #include <bits/stdc++.h> #define ll long long using namespace std; const int N=10; const ll mod=1e9+7; struct AC { AC() { ll a,b,c; while(cin>>a>>b>>c) { if((a&1)&&(b&1)&&(c&1)) cout<<"

ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn&#39;t want to study

262144K Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That means, if he reads books from ll to rr, he wi

ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn&#39;t want to study (线段树)

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That means, if he reads books from ll to rr, he will get a

ACM-ICPC 2018 南京赛区网络预赛 E题

ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems. However, he can submit ii-th problem if and only if he has s

ACM-ICPC 2018 焦作赛区网络预赛 E. Jiu Yuan Wants to Eat (树链剖分-线性变换线段树)

树链剖分若不会的话可自行学习一下. 前两种操作是线性变换,模\(2^{64}\)可将线段树全部用unsigned long long 保存,另其自然溢出. 而取反操作比较不能直接处理,因为其模\(2^{64}\)的特殊性,可将其转化为线性变换. 显然 \[-x\equiv (2^{64}-1)*x (mod\ 2^{64})\] 因为\[!x = (2^{64}-1) -x \] 所以 \[ !x = (2^{64}-1) + (2^{64}-1)x\] #include<bits/stdc++