写了一个离线做法的广义\(\mathrm{SAM}\).
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bitset>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
const int N=1000000+100;
const db pi=acos(-1.0);
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define go(u,i) for (register int i=head[u];i;i=sq[i].nxt)
#define fir first
#define sec second
#define mp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
inline int read()
{
int x=0,f=1;char ch=getchar();
while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
return x*f;
}
namespace My_Math{
#define N 100000
int fac[N+100],invfac[N+100];
int add(int x,int y) {return x+y>=maxd?x+y-maxd:x+y;}
int dec(int x,int y) {return x<y?x-y+maxd:x-y;}
int mul(int x,int y) {return 1ll*x*y%maxd;}
ll qpow(ll x,int y)
{
ll ans=1;
while (y)
{
if (y&1) ans=mul(ans,x);
x=mul(x,x);y>>=1;
}
return ans;
}
int inv(int x) {return qpow(x,maxd-2);}
int C(int n,int m)
{
if ((n<m) || (n<0) || (m<0)) return 0;
return mul(mul(fac[n],invfac[m]),invfac[n-m]);
}
int math_init()
{
fac[0]=invfac[0]=1;
rep(i,1,N) fac[i]=mul(fac[i-1],i);
invfac[N]=inv(fac[N]);
per(i,N-1,1) invfac[i]=mul(invfac[i+1],i+1);
}
#undef N
}
using namespace My_Math;
int n;
char s[N];
struct Trie{
int tot,ch[N][26],fa[N],c[N];
Trie() {tot=1;}
void insert(char *s)
{
int now=1,n=strlen(s+1);
rep(i,1,n)
{
int x=s[i]-'a';
if (!ch[now][x]) {ch[now][x]=(++tot);c[tot]=x;fa[tot]=now;}
now=ch[now][x];
}
}
}tr;
struct Suffix_Automaton{
int tot,fa[N<<1],ch[N<<1][26],len[N<<1],pos[N<<1];
queue<int> q;
Suffix_Automaton() {tot=1;}
int insert(int x,int lst)
{
int np=(++tot),p=lst;len[np]=len[p]+1;
while ((p) && (!ch[p][x])) {ch[p][x]=np;p=fa[p];}
if (!p) {fa[np]=1;return np;}
int q=ch[p][x];
if (len[q]==len[p]+1) {fa[np]=q;return np;}
int nq=(++tot);len[nq]=len[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[nq]));
fa[nq]=fa[q];fa[q]=fa[np]=nq;
while ((p) && (ch[p][x]==q)) {ch[p][x]=nq;p=fa[p];}
return np;
}
void build()
{
rep(i,0,25)
if (tr.ch[1][i]) q.push(tr.ch[1][i]);
pos[1]=1;
while (!q.empty())
{
int u=q.front();q.pop();
//cout << "now " << u << endl;
pos[u]=insert(tr.c[u],pos[tr.fa[u]]);
rep(i,0,25)
if (tr.ch[u][i]) q.push(tr.ch[u][i]);
}
}
ll work()
{
ll ans=0;
rep(i,1,tot) ans+=len[i]-len[fa[i]];
return ans;
}
}sam;
int main()
{
n=read();
rep(i,1,n)
{
scanf("%s",s+1);
tr.insert(s);
}
sam.build();
ll ans=sam.work();
printf("%lld",ans);
return 0;
}