题目大意:给你n个小写字符串 s 1 … s n s_1\dots s_n s1…sn,问有多少字符串 t t t是可接受的。一个串t是可接受的当且仅当存在 t = p 1 + ⋯ + p n t=p_1+\cdots+p_n t=p1+⋯+pn,满足 p i p_i pi是 s i s_i si的子串。 n , ∑ ∣ s ∣ ≤ 1 0 6 n,\sum |s|\le10^6 n,∑∣s∣≤106
题解:显然对每个串构造SAM,每个节点若不存在字符c的出边就向后面第一个初始节点有c出边的SAM的的初始节点的c出边连边然后跑一个拓扑排序即可。
#include
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define Rep(i,v) rep(i,0,(int)v.size()-1)
#define lint long long
#define mod 1000000007
#define ull unsigned lint
#define db long double
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define gc getchar()
#define debug(x) cerr<<#x<<"="<
#define sp <<" "
#define ln <
using namespace std;
typedef pair<int,int> pii;
typedef set<int>::iterator sit;
inline int inn()
{
int x,ch;while((ch=gc)<'0'||ch>'9');
x=ch^'0';while((ch=gc)>='0'&&ch<='9')
x=(x<<1)+(x<<3)+(ch^'0');return x;
}
const int N=1e6+2e5,T=N*3;
namespace SAM_space{
int ch[T][26],fa[T],val[T],node_cnt;
struct SAM{
int rt,las;
inline int init() { return las=rt=new_node(0); }
inline int new_node(int v) { int x=++node_cnt;val[x]=v;return x; }
inline int extend(int w)
{
int p=las,np=new_node(val[p]+1);
while(p&&!ch[p][w]) ch[p][w]=np,p=fa[p];
if(!p) fa[np]=rt;
else{
int q=ch[p][w],v=val[p]+1;
if(val[q]==v) fa[np]=q;
else{
int nq=new_node(v);
fa[nq]=fa[q],fa[q]=fa[np]=nq;
rep(i,0,25) ch[nq][i]=ch[q][i];
while(p&&ch[p][w]==q) ch[p][w]=nq,p=fa[p];
}
}
return las=np;
}
};
}using SAM_space::SAM;SAM s[N];int go[T][26],in[T],vis[T];
inline int add_edge(int x,int y,int i) { return go[x][i]=y,in[y]++; }
inline int build(int x,int n,int *nt)
{
using SAM_space::ch;if(vis[x]) return 0;vis[x]=1;
rep(i,0,25)
if(ch[x][i]) add_edge(x,ch[x][i],i),build(ch[x][i],n,nt);
else if(nt[i]<=n) add_edge(x,ch[s[nt[i]].rt][i],i);
return 0;
}
queue<int> q;int dp[T];
inline int solve()
{
while(!q.empty()) q.pop();dp[1]=1;int ans=0;
rep(i,1,SAM_space::node_cnt) if(!in[i]) q.push(i);
while(!q.empty())
{
int x=q.front();q.pop();
ans+=dp[x],(ans>=mod?ans-=mod:0);
rep(i,0,25)
{
int y=go[x][i];dp[y]+=dp[x],
(dp[y]>=mod?dp[y]-=mod:0);
if(!(--in[y])) q.push(y);
}
}
return ans;
}
char toc[30];int toi[300];
char str[N];int nxt[N][26],nt[30];
int main()
{
// freopen("data.in","r",stdin);
int n=inn();s[0].init();
rep(i,'a','z') toi[i]=i-'a',toc[i-'a']=char(i);
rep(i,1,n)
{
scanf("%s",str+1);int len=(int)strlen(str+1);s[i].init();
rep(j,1,len) nxt[i][toi[(int)str[j]]]=i,s[i].extend(toi[(int)str[j]]);
}
rep(i,0,25) nxt[n+1][i]=n+1;
for(int i=n;i>=0;i--) rep(j,0,25) if(!nxt[i][j]) nxt[i][j]=nxt[i+1][j];
rep(i,0,n) { rep(j,0,25) nt[j]=nxt[i+1][j];build(s[i].rt,n,nt); }
return !printf("%d\n",solve());
}