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 <iostream>//AC自动机模板 
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#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<ll, ll> P;
const int Inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 5e5+100;
template <typename T> 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;i<len;i++){
			int c=buf[i]-'a';
			if(!tree[now][c])tree[now][c]=tire();
			now=tree[now][c];
		}
		num[now]++;
	}
	void build(){
		queue<int>Q;
		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<m;i++){
			int c=buf[i]-'a';
			now=tree[now][c];
			int temp=now;
			while(temp!=root){
				ans+=num[temp];
				num[temp]=0;
				temp=fail[temp];
			}
		}
		return ans;
	}
}ac; 
char s[55],t[maxn<<1];

int main()
{
	//ios::sync_with_stdio(false);
    read(T);
    while(T--){
    	read(n);
    	ac.init();
    	while(n--){
    		scanf("%s",s);
    		ac.insert(s);
		}
		ac.build();
		scanf("%s",t);
		printf("%d\n",ac.query(t) );
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值