版权声明:那个,最起码帮我加点人气吧,署个名总行吧 https://blog.csdn.net/qq_41670466/article/details/82942931
一道后缀自动机看的我头疼,加起来总共花了两天吧,差不多算是理解,真是智商捉急啊!!!
题意大致就是给你一个字符串,然后问你有多少个字串出现的次数在给定的范围内。就酱。
如果对后缀自动机不明白的可以看看这几个链接,讲的很详细:http://hihocoder.com/problemset/problem/1441和陈立杰大神的讲稿,推荐。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1 << 17;
char buf[maxn];
typedef long long ll;
struct SAM
{
int len[maxn << 1], link[maxn << 1], ch[maxn << 1][26];//ch[i][j]=c表示第i个节点加上j所表示的字母到达c节点;
int sz, rt, last;
int newnode(int x = 0)
{
len[sz] = x;
link[sz] = -1;
memset(ch[sz], -1, sizeof(ch[sz]));
return sz++;
}
void init()
{
sz = last = 0;
rt = newnode();
}
void extend(int c)
{
int np = newnode(len[last] + 1);
int p;
for (p = last; ~p && ch[p][c] == -1; p = link[p])
ch[p][c] = np;
if (p == -1)
link[np] = rt;
else
{
int q = ch[p][c];
if (len[p] + 1 == len[q])//判断p状态是不是q状态的父节点
link[np] = q;
else
{
int nq = newnode(len[p] + 1);
memcpy(ch[nq], ch[q], sizeof(ch[q]));
link[nq] = link[q];
link[q] = link[np] = nq;
for (; ~p && ch[p][c] == q; p = link[p])
ch[p][c] = nq;
}
}
last = np;
}
int topcnt[maxn], topsam[maxn << 1];
void sort()
{ // 加入串后拓扑排序
memset(topcnt, 0, sizeof(topcnt));
for (int i = 0; i < sz; i++)
topcnt[len[i]]++;
for (int i = 0; i < maxn - 1; i++)
topcnt[i + 1] += topcnt[i];
for (int i = 0; i < sz; i++)
topsam[--topcnt[len[i]]] = i;
}
int dp[maxn];
void solve(int a, int b)
{
for (int i = 0; i < sz; i++) dp[i] = 0;
int u = 0;
for (int i = 0; buf[i]; i++) //先列出每个位置都有一个字符,所以次数为1;
dp[ u = ch[ u ] [ buf[i] - 'A' ] ] = 1;
for (int i = sz - 1; ~i; i--)//逐步累加每个位置上相同的单个字符出现次数
{
int u = topsam[i];
if (~link[u])
dp[link[u]] += dp[u];//假如一个字符串有三个字符,那么这个字符串就要三个字串(3-0=3);
}
ll ans = 0;
for (int i = 1; i < sz; i++)
if (dp[i] >= a && dp[i] <= b)
ans += len[i] - len[link[i]];
printf("%lld\n", ans);
}
} sam;
int main()
{
int a, b;
while (~scanf("%s%d%d", buf, &a, &b))
{
sam.init();
for (int i = 0; buf[i]; i++)
sam.extend(buf[i] - 'A');
sam.sort();
sam.solve(a, b);
}
return 0;
}
现在就去补拓扑排序,啊!!!!自己好菜啊,怎么什么都不会鸭,为什么大佬什么都知道!!