#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define INF 2147483647
#define mem(i,j) memset(i,j,sizeof(i))
#define F(i,j,n) for(register int i=j;i<=n;i++)
using namespace std;
int n,sn[1000010][28],cnt=0;
int num[1000010],Next[1000010];
char s1[1000010],s2[1000010];
int lst[1000010];
inline int read(){
int datta=0;char chchc=getchar();bool okoko=0;
while(chchc<'0'||chchc>'9'){if(chchc=='-')okoko=1;chchc=getchar();}
while(chchc>='0'&&chchc<='9'){datta=datta*10+chchc-'0';chchc=getchar();}
return okoko?-datta:datta;
}
struct AC_Automaton{
void ins(){
int now=0,len=strlen(s1+1);
F(p,1,len){
if(!sn[now][s1[p]-'a'+1])
sn[now][s1[p]-'a'+1]=++cnt;
now=sn[now][s1[p]-'a'+1];
}
num[now]++;
}
void get_Next(){
int hd=1,tl=0;
F(p,1,26)
if(sn[0][p])
lst[++tl]=sn[0][p];
while(hd<=tl){
int now=lst[hd];
F(p,1,26)
if(!sn[now][p])
sn[now][p]=sn[Next[now]][p];
else{
Next[sn[now][p]]=sn[Next[now]][p];
lst[++tl]=sn[now][p];
}
hd++;
}
}
int ask(){
int now=0,res=0,len=strlen(s2+1);
F(p,1,len){
now=sn[now][s2[p]-'a'+1];
int tnow=now;
while(tnow!=0&&num[tnow]!=-1){
res+=num[tnow];
num[tnow]=-1;
tnow=Next[tnow];
}
}
return res;
}
}ac;
int main(){
n=read();
F(i,1,n)
scanf("%s",s1+1),ac.ins();
ac.get_Next();
scanf("%s",s2+1);
printf("%d",ac.ask());
return 0;
}
原文地址:https://www.cnblogs.com/hzf29721/p/10255641.html
时间: 2024-11-03 13:52:35