您的位置:首页 > 其它

【SPOJ】Distinct Substrings(后缀自动机)

2018-02-13 11:34 169 查看

【SPOJ】Distinct Substrings(后缀自动机)

题面

Vjudge

题意:求一个串的不同子串的数量

题解

对于这个串构建后缀自动机之后

我们知道每个串出现的次数就是\(right/endpos\)集合的大小

但是实际上我们没有任何必要减去不合法的数量

我们只需要累加每个节点表示的合法子串的数量即可

这个值等于\(longest-shortest+1=longest-parent.longest\)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 1000100
inline int read()
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
char ch[MAX];
ll ans;
int tot=1,last=1;
struct Node
{
int son[26];
int ff,len;
}t[MAX<<1];
void extend(int c)
{
int p=last,np=++tot;last=np;
t[np].len=t[p].len+1;
while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
if(!p)t[np].ff=1;
else
{
int q=t[p].son[c];
if(t[p].len+1==t[q].len)t[np].ff=q;
else
{
int nq=++tot;
t[nq]=t[q];t[nq].len=t[p].len+1;
t[q].ff=t[np].ff=nq;
while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
}
}
}
int main()
{
int T=read();
while(T--)
{
scanf("%s",ch+1);
ans=0;last=tot=1;memset(t,0,sizeof(t));
for(int i=1,l=strlen(ch+1);i<=l;++i)extend(ch[i]-65);
for(int i=1;i<=tot;++i)ans+=t[i].len-t[t[i].ff].len;
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: