Luogu P2408 不同子串个数
\(SA\) 总数-重复出现的次数。
$\frac{n\times (n+1)}{2} - \sum_{i=1}^n ht[i] $
#include<iostream>
#include<cstdio>
#include<cstring>
#define R register int
#define ll long long
using namespace std;
namespace Luitaryi {
inline int g() { R x=0,f=1;
register char s; while(!isdigit(s=getchar())) f=s=='-'?-1:f;
do x=x*10+(s^48); while(isdigit(s=getchar())); return x*f;
} const int N=50010;
int T,n,m,c[N],sa[N],rk[N],ht[N],x[N],y[N];
char s[N]; ll ans;
inline void get_sa() {
memset(c,0,sizeof c);
m=123;
for(R i=1;i<=n;++i) ++c[x[i]=s[i]];
for(R i=2;i<=m;++i) c[i]+=c[i-1];
for(R i=n;i;--i) sa[c[x[i]]--]=i;
for(R i=1;i<=n;++i) cout<<sa[i]<<' '; cout<<endl;
for(R t=1,top=0;top<n;m=top,t<<=1) {
top=0;
for(R i=n-t+1;i<=n;++i) y[++top]=i;
for(R i=1;i<=n;++i) if(sa[i]>t) y[++top]=sa[i]-t;
memset(c,0,m+1<<2);
for(R i=1;i<=n;++i) ++c[x[i]];
for(R i=2;i<=m;++i) c[i]+=c[i-1];
for(R i=n;i;--i) sa[c[x[y[i]]]--]=y[i];
swap(x,y),x[sa[1]]=top=1;
for(R i=2;i<=n;++i)
x[sa[i]]=(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+t]==y[sa[i-1]+t])?top:++top;
cout<<p<<endl;
} for(R i=1;i<=n;++i) rk[sa[i]]=i;
}
inline void get_ht() {
R k=0;
for(R i=1,j;i<=n;++i) {
if(k) --k; j=sa[rk[i]-1];
while(s[i+k]==s[j+k]) ++k;
ans-=k;
}
}
inline void main() {
T=g(); while(T--) {
scanf("%s",s+1),n=strlen(s+1);
get_sa(),ans=1ll*n*(n+1)/2,get_ht();
printf("%lld\n",ans);
}
}
} signed main() {Luitaryi::main(); return 0;}
2019.01.09