https://nanti.jisuanke.com/t/A2018
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
又学了一天,稍微懂点了,哭了
后缀自动机初学
https://blog.csdn.net/AcTarjan/article/details/82859964
https://blog.csdn.net/enjoy_pascal/article/details/82429963
https://blog.csdn.net/liyuanshuo_nuc/article/details/53561527
后缀自动机的一些应用
https://blog.csdn.net/As_A_Kid/article/details/80183420
https://blog.csdn.net/myjs999/article/details/81174680
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
const int maxc=26;
char s[maxn];
int A,B;
struct Suffix_Automaton {//打*的不一定用到
int next[maxn<<1][maxc]; //状态转移(尾部加一个字符的下一个状态)
int len[maxn<<1]; //最长子串的长度(该节点子串数量=len[x]-len[link[x]])
int link[maxn<<1]; //后缀链接(最短串前部减少一个字符所到达的状态)
//int cnt[maxn<<1]; //被后缀连接的数(*)
int id; //结点编号
int last; //最后结点
ll endpos[maxn<<1]; // endpos数(一类子串的数量)
int a[maxn];
int b[maxn<<1];
ll d[maxn<<1];//d[i]表示从状态i出发,不同的子串的数目,即不同的路径数
void init() { //初始化
for(int i=1; i<=id; i++){ //常规初始化
link[i] = len[i] = 0;
memset(next[i],0,sizeof(next[i]));
endpos[i]=0;
a[i]=0;
b[i]=0;
}
for(int i=1;i<=id;i++) {//非常规初始化
d[i]=0;
}
last = id = 1; //1表示root起始点 空集
}
//SAM建图
void add(int c) { //插入字符,为字符ascll码值
int x = ++id; //创建一个新结点x;
len[x] = len[last] + 1; // 长度等于最后一个结点+1
endpos[x] = 1; //接受结点子串除后缀连接还需加一
int p; //第一个有C转移的结点;
for (p = last; p && !next[p][c]; p = link[p])
next[p][c] = x;//沿着后缀连接 将所有没有字符c转移的节点直接指向新结点
if (!p){ //全部都没有c的转移 直接将新结点后缀连接到起点
link[x] = 1;
// cnt[1]++;
}
else {
int q = next[p][c]; //p通过c转移到的结点
if (len[p] + 1 == len[q]){//pq是连续的
link[x] = q;
// cnt[q]++; //将新结点后缀连接指向q即可,q结点的被后缀连接数+1
}
else {
int nq = ++id; //不连续 需要复制一份q结点
len[nq] = len[p] + 1; //令nq与p连续
link[nq] = link[q]; //因后面link[q]改变此处不加cnt
memcpy(next[nq], next[q], sizeof(next[q])); //复制q的信息给nq
for (; p&&next[p][c] == q; p = link[p])
next[p][c] = nq; //沿着后缀连接 将所有通过c转移为q的改为nq
link[q] = link[x] = nq; //将x和q后缀连接改为nq
//cnt[nq] += 2; // nq增加两个后缀连接
}
}
last = x; //更新最后处理的结点
}
ll getSubNum() { //求不相同子串数量
ll ans = 0;
for (int i = 2; i <= id; i++)
ans += len[i]-len[link[i]]; //一状态子串数量等于len[i]-len[link[i]]
return ans;
}
void getTP(int Len){//对sam的节点按照len,从小到大排序重新标号,即给定节点的拓扑序
for(int i=1;i<=id;i++) a[len[i]]++;
for(int i=1;i<=Len;i++) a[i]+=a[i-1];
for(int i=1;i<=id;i++) b[a[len[i]]--]=i;
}
void getendpos(){//求每类子串的数量 ,即endpos集合的大小
for(int i=id;i>=1;i--){ //按拓扑序遍历
int e=b[i];
endpos[link[e]]+=endpos[e];
}
}
void solve1(){
ll ans=0;
for(int i=1;i<=id;i++){
if(endpos[i]>=A&&endpos[i]<=B){
ans+=len[i]-len[link[i]];
}
}
printf("%lld\n",ans);
}
void solve2(){
for(int i=id;i>1;i--){
int v=b[i];
if(endpos[v]>=A&&endpos[v]<=B) d[v]++;
for(int j=0;j<26;j++){
if(next[v][j]) d[v]+=d[next[v][j]];
}
}
ll ans=0;
for(int i=0;i<26;i++){
if(next[1][i]) ans+=d[next[1][i]];
}
printf("%lld\n",ans);
}
} sam;
int main(){
while(~scanf("%s%d%d",s,&A,&B)){
sam.init();
int len=strlen(s);
for(int i=0;i<len;i++) sam.add(s[i]-'A');
sam.getTP(len);
sam.getendpos();
sam.solve1();
//sam.solve2();
}
return 0;
}