大体上与第一篇题解相似,但下标从1开始
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define re register
快读略
char s[1001000];
struct tree
{
int son[30];
int nxt;
int end;
}t[1000100];
int cnt=0;
void qwq()
{
int len=strlen(s+1);
int now=0;
for (int i=1;i<=len;++i)
{
int awa=s[i]-'a'+1;
if (t[now].son [awa])
{
now=t[now].son [awa];
}
else
{
t[now].son [awa]=++cnt;
now=cnt;
}
}
++t[now].end ;
}
queue<int>q;
void getfail()
{
q.push(0);t[0].nxt =0;
for(int i=1;i<=26;++i)//第二层的fail指针提前处理一下
{
if(t[0].son [i]!=0)
{
t[ t[0].son [i]].nxt =0;
q.push(t[0].son [i]);
}
}
while (q.empty() ==false)
{
int now=q.front() ;q.pop() ;
for (int i=1;i<=26;++i)
{
if (t[now].son [i])
{
t[ t[now].son [i] ].nxt =t[ t[now].nxt ].son [i];
q.push((t[now].son [i]));
}
else
{
t[now].son [i]=t[ t[now].nxt ].son [i];
}
}
}
}
int cmp()
{
int len=strlen(s+1);
int now=0;int ans=0;
for (int i=1;i<=len;++i)
{
int awa=s[i]-'a'+1;
now=t[now].son [awa];
for (int tmp=now;t[now].end !=-1 && tmp!=0;tmp=t[tmp].nxt )
{
ans+=t[tmp].end ;
t[tmp].end =-1;
}
}
return ans;
}
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int n;n=read();
for (int i=1;i<=n;++i)
{
scanf("%s",s+1);
qwq();
}
getfail();
scanf("%s",s+1);
cout<<cmp();
fclose(stdin);
fclose(stdout);
return 0;
}