AC自动机模板[雾]
#include <bits/stdc++.h>
using namespace std;
int n,t=1,trie[1000000][26];
int fail[1000000];
int cntword[1000000];
void insert(string& str)
{
int root=0;
int len=str.length();
for(int i=0;i<len;i++)
{
if(trie[root][str[i]-'a']==0)
{
t++;
trie[root][str[i]-'a']=t;
}
root=trie[root][str[i]-'a'];
}
cntword[root]++;
}
void get_fail()
{
queue<int> q;
fail[0]=0;
for(int i=0;i<26;i++)
{
if(trie[0][i])
{
q.push(trie[0][i]);
fail[trie[0][i]]=0;
}
}
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=0;i<26;i++)
{
if(trie[now][i])
{
fail[trie[now][i]] = trie[fail[now]][i];
q.push(trie[now][i]);
}
else
{
trie[now][i] = trie[fail[now]][i];
}
}
}
}
int query(string str)
{
int len=str.length(),now=0,ans=0;
for(int i=0;i<len;i++)
{
now=trie[now][str[i]-'a'];
for(int j=now;j&&cntword[j]!=-1;j=fail[j])
{
ans+=cntword[j];
cntword[j]=-1;
}
}
return ans;
}
int main()
{
scanf("%d",&n);
string str;
for(int i=1;i<=n;i++)
{
cin>>str;
insert(str);
}
get_fail();
cin>>str;
printf("%d",query(str));
}