【AC自动机】 HDOJ 2222 Keywords Search

AC自动机模板题,网上的好多AC自动机的例子都是用这道题滴。。。汲取没学好KMP的教训,这里讲一下我对AC自动机的理解,一来加深我对AC自动机的理解,二来希望可以帮助读者理解。。

先给出我写的数据结构。。

struct node
{
	int isword;
	struct node *fail, *next[26];
	node() {
		isword = 0;
		fail = NULL;
		for(int i = 0; i < 26; i++)
			next[i] = NULL;
	}
}*root, *tmp, *now, *p;
queue<node*> q;

isword代表这个单词的个数(因为这道题输入好像有重复的单词),fail是失配指针,其他写的和字典树一样。STL队列用于BFS搜索建树。

编程的时候,先写个字典树。。

void inseart(void)
{
	int len = strlen(s), i, temp;
	now = root;
	for(i = 0; i < len; i++) {
		temp = s[i] - 'a';
		if(now->next[temp] == NULL)
			now->next[temp] = new struct node;
		now = now->next[temp];
	}
	now->isword++;
}

下一步就是用BFS找出树上每一个节点的fail指针。开始的时候把根节点丢到队列里,把根节点的fail指针设为NULL。每次从队列里取出队列头,然后对这个节点的所有子节点找到它们的fail指针。具体操作是这样的:设每次从队列里取出来的节点为当前节点,那么当前节点只有两种情况,一种是当前节点就是根节点,另一种是当前节点不是根节点。如果是第一种,就把它的所有子节点的fail指针设为根节点。如果是第二种的话就从当前节点的失配指针开始向前找,找啊找啊找啊找,一直找到某个节点的存在同样的子节点,然后就把当前节点的子节点的fail指针指向那个节点的子节点。如果找不到,那么就把当前节点的子节点的fail指针指向根节点。。。。。。貌似这种方法效率不高的样子,据说可以对找的过程用类似并查集的路径压缩的方法优化。。不过我现在不会。。

void build(void)
{
	int i;
	root->fail = NULL;
	q.push(root);
	while(!q.empty()) {
		now = q.front();
		q.pop();
		for(i = 0; i < 26; i++) {
			if(now->next[i] == NULL) continue;
			if(now == root) now->next[i]->fail = root;
			else {
				p = now->fail;
				while(p) {
					if(p->next[i]) {
						now->next[i]->fail = p->next[i];
						break;
					}
					p = p->fail;
				}
				if(!p) now->next[i]->fail = root;
			}
			q.push(now->next[i]);
		}
	}
}

酱紫建树完成了!!然后就是匹配的过程:每次从要匹配的字符串取出一个字符。。如果当前节点没有这个子节点,那么就沿着失配边走,然后就看看走到的那个节点的isword,有多少答案就加多少上去,然后更新isword(防止重复加)。最后输出答案就行啦~~

void search(void)
{
	int len = strlen(s), ans=0, i, temp;
	now = root;
	for(i = 0; i < len; i++) {
		temp = s[i] - 'a';
		while(now != root && !now->next[temp]) now = now->fail;
		if(now->next[temp]) {
			now = now->next[temp];
			p = now;
			while(p != root && p->isword) {
				ans += p->isword;
				p->isword = 0;
				p = p->fail;
			}
		}
	}
	printf("%d\n", ans);
}

整个代码就酱紫啦。。。

#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <climits>
#define maxn 1000005
#define eps 1e-6
#define mod 10007
#define INF 99999999
#define lowbit(x) (x&(-x))
//#define lson o<<1, L, mid
//#define rson o<<1 | 1, mid+1, R
typedef long long LL;
using namespace std;

struct node
{
	int isword;
	struct node *fail, *next[26];
	node() {
		isword = 0;
		fail = NULL;
		for(int i = 0; i < 26; i++)
			next[i] = NULL;
	}
}*root, *tmp, *now, *p;
queue<node*> q;
char s[maxn];
int n;

void inseart(void)
{
	int len = strlen(s), i, temp;
	now = root;
	for(i = 0; i < len; i++) {
		temp = s[i] - 'a';
		if(now->next[temp] == NULL)
			now->next[temp] = new struct node;
		now = now->next[temp];
	}
	now->isword++;
}
void read(void)
{
	int i;
	scanf("%d", &n);
	root = new struct node;
	for(i = 0; i < n; i++) {
		scanf("%s", s);
		inseart();
	}
	scanf("%s", s);
}
void build(void)
{
	int i;
	root->fail = NULL;
	q.push(root);
	while(!q.empty()) {
		now = q.front();
		q.pop();
		for(i = 0; i < 26; i++) {
			if(now->next[i] == NULL) continue;
			if(now == root) now->next[i]->fail = root;
			else {
				p = now->fail;
				while(p) {
					if(p->next[i]) {
						now->next[i]->fail = p->next[i];
						break;
					}
					p = p->fail;
				}
				if(!p) now->next[i]->fail = root;
			}
			q.push(now->next[i]);
		}
	}
}
void search(void)
{
	int len = strlen(s), ans=0, i, temp;
	now = root;
	for(i = 0; i < len; i++) {
		temp = s[i] - 'a';
		while(now != root && !now->next[temp]) now = now->fail;
		if(now->next[temp]) {
			now = now->next[temp];
			p = now;
			while(p != root && p->isword) {
				ans += p->isword;
				p->isword = 0;
				p = p->fail;
			}
		}
	}
	printf("%d\n", ans);
}
int main(void)
{
	int _;
	while(scanf("%d", &_)!=EOF) {
		while(_--){
			read();
			build();
			search();
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值