Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 79890 Accepted Submission(s): 27827
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
ac:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<queue>
#define MAXN 1000005
using namespace std;
const int N=10100;
char str[MAXN];
int num,n;
struct node
{
int son[30];
int fail,cnt;
}ac[N*60];
queue<int> que;
void init(int x)
{
ac[x].cnt=0;
ac[x].fail=0;
memset(ac[x].son,0,sizeof(ac[x].son));
}
void trie(char ctr[])
{
int len=strlen(ctr);
int x=0;
for(int i=0;i<len;i++)
{
int t=ctr[i]-'a'+1;
if(!ac[x].son[t])
{
num++;
init(num);
ac[x].son[t]=num;
}
x=ac[x].son[t];
}
ac[x].cnt++;
}
void buildac()
{
while(!que.empty())
que.pop();
for(int i=1;i<=26;i++)
if(ac[0].son[i])
que.push(ac[0].son[i]);
while(!que.empty())
{
int x=que.front();
que.pop();
int fail=ac[x].fail;
for(int i=1;i<=26;i++)
{
int y=ac[x].son[i];
if(y)
{
ac[y].fail=ac[fail].son[i];
que.push(y);
}
else ac[x].son[i]=ac[fail].son[i];
}
}
}
int find1(char ctr[])
{
int len=strlen(ctr);
int x=0,ans=0;
for(int i=0;i<len;i++)
{
int t=ctr[i]-'a'+1;
while(x&&!ac[x].son[t])
x=ac[x].fail;
x=ac[x].son[t];
int p=x;
while(p&&ac[p].cnt!=-1)
{
ans+=ac[p].cnt;
ac[p].cnt=-1;
p=ac[p].fail;
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
num=0;
init(0);
for(int i=1;i<=n;i++)
{
scanf("%s",&str);
trie(str);
}
buildac();
scanf("%s",str);
printf("%d\n",find1(str));
}
}
下面是kuangbin大佬的模板:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=26;
const int MAXN=500005;
struct Trie{
int next[MAXN][N],fail[MAXN],end[MAXN];
int root;
int tot;
int newnode()
{
for(int i=0;i<N;i++)
next[tot][i]=-1;
end[tot++]=0;
return tot-1;
}
void init()
{
tot=0;
root=newnode();
}
void insert(char buf[])
{
int len=strlen(buf);
int now=root;
for(int i=0;i<len;i++)
{
int k=buf[i]-'a';
if(next[now][k]==-1)
next[now][k]=newnode();
now=next[now][k];
}
end[now]++;
}
void build()
{
queue<int> que;
fail[root]=root;
for(int i=0;i<N;i++)
if(next[root][i]==-1)
next[root][i]=root;
else
{
fail[next[root][i]]=root;
que.push(next[root][i]);
}
while(!que.empty())
{
int now = que.front();
que.pop();
for(int i=0;i<N;i++)
if(next[now][i]==-1)
next[now][i]=next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
que.push(next[now][i]);
}
}
}
int query(char buf[])
{
int len=strlen(buf);
int now=root;
int res=0;
for(int i=0;i<len;i++)
{
now=next[now][buf[i]-'a'];
int temp=now;
while(temp!=root)
{
res+=end[temp];
end[temp]=0;//模式串只在主串中匹配一次就可以了
temp=fail[temp];
}
}
return res;
}
};
Trie ac;
char buf[MAXN+MAXN];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
ac.init();
for(int i=0;i<n;i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
}
return 0;
}