hdu2222(AC自动机入门模板题)

题目链接:https://vjudge.net/problem/HDU-2222

Keywords Search

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自动机入门题。

AC自动机简介: 
首先简要介绍一下AC自动机:Aho-Corasick automation,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一。一个常见的例子就是给出n个单词,再给出一段包含m个字符的文章,让你找出有多少个单词在文章里出现过。要搞懂AC自动机,先得有字典树Trie和KMP模式匹配算法的基础知识。KMP算法是单模式串的字符匹配算法,AC自动机是多模式串的字符匹配算法。

AC自动机的构造:
1.构造一棵Trie,作为AC自动机的搜索数据结构。
2.构造fail指针,使当前字符失配时跳转到具有最长公共前后缀的字符继续匹配。如同 KMP算法一样, AC自动机在匹配时如果当前字符匹配失败,那么利用fail指针进行跳转。由此可知如果跳转,跳转后的串的前缀,必为跳转前的模式串的后缀并且跳转的新位置的深度(匹配字符个数)一定小于跳之前的节点。所以我们可以利用 bfs在 Trie上面进行 fail指针的求解。
3.扫描主串进行匹配。
具体讲解详见博客:https://blog.csdn.net/bestsort/article/details/82947639

此题示例代码

#include //AC自动机模板 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define cla(a, sum) memset(a, sum, sizeof(a))
#define rap(i, m, n) for(int i=m; i<=n; i++)
#define rep(i, m, n) for(int i=m; i>=n; i--)
#define bug printf("???\n")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair P;
const int Inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 5e5+100;
template  void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}


int T,n;
struct Tire{
	int tree[maxn][26],num[maxn];
	int fail[maxn];
	int cnt,root;//root代表根节点 
	int tire(){
		rap(i,0,25)tree[cnt][i]=0;
		num[cnt++]=0;
		return cnt-1;
	}
	void init(){
		cnt=0;
		root=tire();
	}
	void insert(char buf[]){
		int len=strlen(buf);
		int now=root;
		for(int i=0;iQ;
		rap(i,0,25){
			if(tree[root][i]){
				fail[tree[root][i]]=root;
				Q.push(tree[root][i]); 
			}
			else tree[root][i]=root;
		} 
		while(!Q.empty() ){
			int now=Q.front() ;
			Q.pop() ;
			rap(i,0,25){
				if(tree[now][i]){
					fail[tree[now][i]]=tree[fail[now]][i];
					Q.push(tree[now][i]);
				}
				else tree[now][i]=tree[fail[now]][i];
			}
		}
	}
	int query(char buf[]){
		int m=strlen(buf);
        int now=root,ans=0;
		for(int i=0;i

 

你可能感兴趣的:(串与序列,数据结构,#,AC自动机)