Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 44959 Accepted Submission(s): 14215
Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
Output
Print how many keywords are contained in the description.
Sample Input
1
5
she
he
say
shr
her
yasherhs
Sample Output
3
#include<cstdio>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef __int64 ll;
const int N=26;
const int M=1000005;
const int L=52;
typedef struct Node
{
int count;
Node *Next[N];
Node *fail;
Node *last;
}Node;
Node * build()//建立节点
{
Node *node=(Node *)malloc(sizeof(Node));
node->count=0;
node->fail=NULL;
node->last=NULL;
memset(node->Next,NULL,sizeof(node->Next));
return node;
}
void actire_insert(Node *root,char str[])//日常插入
{
Node *p=root;
int id,i,len=strlen(str);
for(i=0;i<len;i++)
{
id=str[i]-'a';
if(p->Next[id]==NULL)
p->Next[id]=build();
p=p->Next[id];
}
p->count++;
}
//zi
void get_fail(Node *root)
{
queue<Node *>q;
q.push(root);//先将root入队
int i;
while(!q.empty())
{
Node *p=q.front();
q.pop();
for(i=0;i<N;i++)
{//根表示弹出队列的 p
if(p->Next[i]!=NULL)//找到存在相应的根的节点
{
if(p==root) p->Next[i]->fail=root;//此时如果根节点是root,就让根的节点的失败指针指向root
else//否则
{
Node *temp=p->fail;
//定义临时指针等于当前根节点的失败指针,这里是因为要找有最长公共前后缀继续匹配;
while(temp!=NULL)
{
if(temp->Next[i]!=NULL)
{
p->Next[i]->fail=temp->Next[i];
break;
}
if(temp->Next[i]==NULL)
temp=temp->fail;//如果根节点的失败指针没有与之相同的,继续循环,直到temp等于NULL
}
if(temp==NULL)
p->Next[i]->fail=root;
}
q.push(p->Next[i]);
}
}
}
}
int found(Node *root,char str[])
{
int id,len=strlen(str),ans=0;
Node *p=root;
for(int i=0;i<len;i++)
{
id=str[i]-'a';
while(p->Next[id]==NULL&&p!=root)
p=p->fail;
p=p->Next[id];
if(p==NULL)
p=root;
Node *temp=p;
while(temp!=root&&temp->count!=-1)
{
ans+=temp->count;
temp->count=-1;
temp=temp->fail;
}
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
Node *root=build();
int n,i;
scanf("%d",&n);
char s[L],str[M];
for(i=0;i<n;i++)
{
scanf("%s",s);
actire_insert(root,s);
}
get_fail(root);
scanf("%s",str);
printf("%d\n",found(root,str));
}
return 0;
}