G - Keywords Search
Crawling in process...
Crawling failed
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u
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.
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.
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 <algorithm>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;
#define LEN 55
#define WOR 125
#define MAX 1000005
char str[MAX];
char po[LEN];
struct node
{
int count;
node* fail;
node* next[26];
node()
{
count=0;
fail=NULL;
for(int i=0; i<26; ++i)
next[i]=NULL;
}
};
node* List[MAX];
node* root;
void build_AC()
{
int head=0,now=0;
List[head++]=root;
while(head!=now)
{
// cout<<"fuck\n";
node* p=List[now++];
node* temp=NULL;
for(int i=0; i<26; ++i)
{
// cout<<"fuck\n";
if(p->next[i])
{
if(p==root)
p->next[i]->fail=root;
else
{
temp=p->fail;
while(temp)
{
if(temp->next[i])
{
p->next[i]->fail = temp->next[i];
break;
// cout<<"this\n";
}
temp=temp->fail;
}
if(!temp)
p->next[i]->fail=root;
}
List[head++]=p->next[i];
}
}
}
}
int query()
{
int now,poi,sum=0;
int len=strlen(str);
node *p=root;
for(int i=0; i<len; ++i)
{
poi=str[i]-'a';
while(!p->next[poi] && p!=root)
p=p->fail;
p=p->next[poi];
if(p==NULL)
{
p=root;
//continue;
}
node* temp=p;
while(temp!=root &&temp->count!=-1)
{
sum+=temp->count;
temp->count=-1;
temp=temp->fail;
// cout<<"poi\n";
}
// cout<<"niko\n";
}
return sum;
}
void in_sert(char* s)
{
node* p=root;
int len = strlen(s);
int poi;
for(int i=0; i<len; ++i)
{
poi=s[i]-'a';
if(!p->next[poi])
p->next[poi]=new node();
p=p->next[poi];
}
p->count++;
}
int main()
{
int T,n;
cin>>T;
while(T--)
{
int i,j,k;
scanf("%d",&n);
root=new node();
for(i=0; i<n; ++i)
{
scanf("%s",po);
in_sert(po);
}
scanf("%s",str);
build_AC();
int num=query();
printf("%d\n",num);
}
return 0;
}