Ac自动机 c++

什么是Ac自动机

学会Ac自动机前提是要知道tire树和kmp的实现和原理

Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法。
要学会AC自动机,我们必须知道什么是Trie,也就是字典树。Trie树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。
一个常见的例子就是给出n个单词,再给出一段包含m个字符的文章,让你找出有多少个单词在文章里出现过。

Ac自动机的实现

  1. 构造trie树
tr[N][M]//以N为结点的M个儿子节点
cnt[N]//以N为结尾元素的字符串的个数
void insert()
{ 
	int p=0;
	for(int i=0;str[i];i++)
	{
		int t = str[i]-'a';
		if(!tr[p][t]) tr[p][t] = ++idx;
		p = tr[p][t];
	}
	cnt[p]++;
}
  1. 构造类似kmp中next的数组
void build()
{
	queue<int> qu;
	//将所有trie树中第一层的结点加入队列
	for(int i=0;i<26;i++)
		if(tr[0][i]) qu.push(tr[0][i]);
	
	whille(qu.size())
	{
		int t = qu.front();
		qu.pop();
		//枚举当前队头的26个分支
		for(int i=0;i<26;i++)
		{
			int p = tr[t][i];
			//如果存在我们就让它的ne指针指向他父亲节点的 ne指针指向的那个节点(根)的具有相同字母的子节点
			if(p)
			{
				tr[t][i] = tr[ne[t]][i];
				qu.push(tr[t][i])
			}
			//就算不存在,不跳,他的树节点值也等于父节点的ne指向的节点中具有相同字母的子节点
			else 
			{
				tr[t][i] = tr[ne[t]][i];
			}
			
		}
	}
}
  1. 实现匹配函数
int res = 0;
for(int i=0,j=0;str[i];++i)
{
	int u = str[i]-'a';
	j = tr[j][u];
	int p=j;
	while(p){
		res+=cnt[p];
		cnt[p] = 0;
		p = ne[p];
	}
	cout<<res<<endl;
}

模板例题

在这里插入图片描述
代码如下

#include<bits/stdc++.h>
using namespace std;

const int N = 10010,S=55,M=1000010;
int n;
int tr[N*S][26],cnt[N*S],idx;
char str[M];
int q[N*S],ne[N*S];

void insert()
{
    int p=0;
    for(int i=0;str[i];i++)
    {
        int t = str[i]-'a';
        if(!tr[p][t]) tr[p][t] = ++idx;
        p = tr[p][t];
    }
    cnt[p] ++ ;
}

void build()
{
    queue<int> qu;
    for(int i=0;i<26;i++)
        if(tr[0][i])
            qu.push(tr[0][i]);
            
    while(qu.size())
    {
        int t = qu.front();
        qu.pop();
        for(int i=0;i<26;i++)
        {
            int p=tr[t][i];
            if(!p) tr[t][i] = tr[ne[t]][i];
            else
            {
                ne[p] = tr[ne[t]][i];
                qu.push(p);
            }
        }
    }
        
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        memset(tr,0,sizeof tr);
        memset(cnt,0,sizeof cnt);
        memset(ne,0,sizeof ne);
        
        idx = 0;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>str;
            insert();
        }
        build();
        cin>>str;
        
        int res = 0;
        for(int i=0,j=0;str[i];i++)
        {
            int t = str[i]-'a';
            j = tr[j][t];
            int p=j;
            while(p)
            {
                res+=cnt[p];
                cnt[p]=0;
                p=ne[p];
            }
        }
        cout<<res<<endl;
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ranyh524

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值