没有学过ac自动机的同学 可移步这位大佬的链接 https://blog.csdn.net/bestsort/article/details/82947639
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 85932 Accepted Submission(s): 29875
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
这里讲一下模板的各个代码的作用,加强一下理解。
这个题目还有要注意的地方就是TLE和MLE
输入字符串与字典树的大小为两倍关系,开大了会超。
不要用string 用char *读入,strlen要写在for循环外
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#include <bitset>
#define LL long long
#define ULL unsigned long long
#define mod 10007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int maxn = 10000 * 50 + 10;
int n,m;
int temp[maxn][26];
int colo[maxn];
int fail[maxn];
int cnt;
void buildTire(char str[])//建好字典树
{
int tot = 0;
int len = strlen(str);
for(int i = 0; i < len; i++){
int c = str[i] - 'a';
if(!temp[tot][c]){
temp[tot][c] = ++cnt;
}
tot = temp[tot][c];
}
colo[tot]++;//记录字符串结尾的位置一共有多少个
}
void getFail()//获得失配指针
{
queue<int> Q;
for(int i = 0; i < 26; i++){
if(temp[0][i]){
fail[temp[0][i]] = 0;//第一个字符的失配都指向根节点
Q.push(temp[0][i]);
}
}
while(!Q.empty()){
int v = Q.front();
Q.pop();
for(int i = 0; i < 26; i++){
if(temp[v][i]){
fail[temp[v][i]] = temp[fail[v]][i];//当前字符的指针指向父节点失配指针的子节点
Q.push(temp[v][i]);
}
else{
temp[v][i] = temp[fail[v]][i];//若没有出现该字符,当前字符指向父节点失配指针的子节点
}
}
}
}
int query(char str[])
{
int ans = 0,now = 0;
int len = strlen(str);
for(int i = 0; i < len; i++){
int c = str[i] - 'a';
now = temp[now][c];
for(int j = now; j && colo[j]!=-1; j = fail[j]){
ans += colo[j];
colo[j] = -1;
}
}
return ans;
}
void init()
{
for(int i = 0; i < maxn;i++){
fail[i] = 0,colo[i] = 0;
}
mem(temp,0);
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
cnt = 0;
init();
int n;
scanf("%d",&n);
while(n--){
char str[maxn << 1];
scanf("%s",str);
buildTire(str);
}
fail[0] = 0;
getFail();
char ss[maxn << 1];
scanf("%s",ss);
printf("%d\n",query(ss));
}
return 0;
}