Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)
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
Author
Wiskey
题意:给你n个长度不超过50的小字符串,再给你一个长度不超过1000000的大字符串,问有几个小字符串在大字符串中出现过
题解:AC自动机模板题。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstdlib>
#define LiangJiaJun main
using namespace std;
int T;
int n,sz,ans,ch[1000004][34],p[1000004],cnt[1000004];
char s[54],m[1000004];
bool mark[1000004];
queue<int>q;
int INS(char *s){
int l=strlen(s+1),now=1;
for(int i=1;i<=l;i++){
int to=s[i]-'a';
if(ch[now][to])now=ch[now][to];
else{
now=ch[now][to]=++sz;
}
}
cnt[now]++;
return 0;
}
int build(){
p[1]=0;
while(!q.empty())q.pop();
q.push(1);
while(!q.empty()){
int x=q.front();q.pop();
for(int i=0;i<26;i++){
if(!ch[x][i])continue;
int now=p[x];
while(!ch[now][i])now=p[now];
p[ch[x][i]]=ch[now][i];
q.push(ch[x][i]);
}
}
return 0;
}
int solve(char *pro){
int ret=0,now=1,l=strlen(pro+1);
for(int i=1;i<=l;i++){
mark[now]=1;
int to=pro[i]-'a';
while(!ch[now][to])now=p[now];
now=ch[now][to];
if(!mark[now]){
for(int j=now;j;j=p[j]){
ret+=cnt[j];
cnt[j]=0;
}
}
}
return ret;
}
int w33ha(){
for(int i=1;i<=sz;i++){
mark[i]=p[i]=cnt[i]=0;
for(int j=0;j<26;j++)ch[i][j]=0;
}
scanf("%d",&n);
sz=1;ans=0;
for(int i=0;i<26;i++)ch[0][i]=1;
for(int i=1;i<=n;i++){
scanf("%s",s+1);INS(s);
}
scanf("%s",m+1);
build();
printf("%d\n",solve(m));
return 0;
}
int LiangJiaJun(){
scanf("%d",&T);
while(T--)w33ha();
return 0;
}