还有一个数据很恶心啊,模式串会有一样的,出现一次算两次。这个真的很狗了
#include<iostream>
#include<queue>
#define maxn 1000001
using namespace std;
bool b[maxn];
string word,s;
int ans=0;
typedef struct A{
A *fail;
A *next[26];
int word;
int num;
void a(){
for(int i=0;i<26;i++){
next[i]=NULL;
}
fail=NULL;
word=0;
num=0;
}
}node;
node *root;
void insert_word(int j){
int len=word.length();
node *p=root;
for(int i=0;i<len;i++){
int k=word[i]-'a';
if(p->next[k]==NULL){
node *temp;
temp=new node;
temp->a();
p->next[k]=temp;
}
p=p->next[k];
}
if(p->word==0)p->word=j;
p->num++;
}
void fail(){
queue<node *>q;
q.push(root);
while(!q.empty()){
node *p=q.front();
q.pop();
for(int i=0;i<26;i++){
if(p->next[i]!=NULL){
node *t=p->next[i];
if(p==root){
t->fail=root;
}
else{
node *fafail=p->fail;
while(fafail!=NULL){
if(fafail->next[i]!=NULL){
t->fail=fafail->next[i];
break;
}
fafail=fafail->fail;
}
if(fafail==NULL)t->fail=root;
}
q.push(t);
}
}
}
}
void query(){
int len=s.length();
node *p=root;
for(int i=0;i<len;i++){
int k=s[i]-'a';
while(p!=root&&p->next[k]==NULL)
p=p->fail;
p=p->next[k];
if(p==NULL)
p=root;
node *temp=p;
while(temp!=root){
if(temp->word&&!b[temp->word]){
b[temp->word]=1;
ans+=temp->num;
}
temp=temp->fail;
}
}
}
/*
int c[10001];
int all=0;
void print(){
for(int i=1;i<=all;i++){
printf("%c",b[i]+'a');
}
cout<<endl;
}
void dfs(node *t){
if(t->word)print();
for(int i=0;i<26;i++){
if(t->next[i]!=NULL){
c[++all]=i;
dfs(t->next[i]);
all--;
}
}
}*/
int main(){
//freopen("P3808.in","r",stdin);
//freopen("P3808.out","w",stdout);
root=new node;
root->a();
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>word;
insert_word(i);
}
//dfs(root);
fail();
while(cin>>s){
ans=0;
query();
cout<<ans<<endl;
}
}