AC自动机

一、基本:
当我们遍历到 某个 节点的时候,由于存在这个节点,我们就让他的fail指针 指向 他父亲节点的fail指针指向的那个节点的具有相同字母的子节点。

简单来说,AC自动机是用来进行多模式匹配(单个主串,多个模式串)的高效算法。

使用Aho-Corasick算法需要三步:
建立模式串的Trie
给Trie添加失败路径
根据AC自动机,搜索待处理的文本

例题:Keywords Search HDU - 2222

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

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#define ll long long
using namespace std;
const int maxn=1000010;
int trie[maxn][26];
int sum[maxn];
int fail[maxn];
int cnt=0;

void init(void)
{
    memset(trie,0,sizeof(trie));
    memset(sum,0,sizeof(sum));
    memset(fail,0,sizeof(fail));
    cnt=0;
}

char str[maxn];
char str1[108];

void _insert(void)
{
    int p=0;
    int k;
    int len=strlen(str1);
    for(int i=0;i<len;i++)
    {
        k=str1[i]-'a';
        if(!trie[p][k]) trie[p][k]=++cnt;
        p=trie[p][k];
    }
    sum[p]++;

}

void getfail(void)
{
    queue<int>q;
    for(int i=0;i<26;i++)
    {
        if(trie[0][i])
        {
            fail[trie[0][i]]=0;
            q.push(trie[0][i]);
        }
    }
    while(q.size())
    {
        int now=q.front();
        q.pop();

        for(int i=0;i<26;i++)
        {
            int p=trie[now][i];
            if(p)
            {
                fail[p]=trie[fail[now]][i];
                q.push(p);
            }
            else
            {
                trie[now][i]=trie[fail[now]][i];
            }
        }
    }

}

int query(void)
{
    int p=0,ans=0;
    int len=strlen(str);
    for(int i=0;i<len;i++)
    {
        p=trie[p][str[i]-'a'];

        for(int j=p;j && sum[j]!=-1;j=fail[j])
        {
            ans += sum[j];
            sum[j]=-1;
        }
    }
    return ans;
}

int main(void)
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",str1);
            _insert();
        }

        getfail();

        scanf("%s",str);

        printf("%d\n",query());
    }
    return 0;
}

二、洛谷P3796 AC自动机加强版
题目描述
有N个由小写字母组成的模式串以及一个文本串T。每个模式串可能会在文本串中出现多次。你需要找出哪些模式串在文本串T中出现的次数最多。

输入输出格式
输入格式:
输入含多组数据。

每组数据的第一行为一个正整数N,表示共有N个模式串,1≤N≤150。

接下去N行,每行一个长度小于等于70的模式串。下一行是一个长度小于等于10^6 的文本串T。

输入结束标志为N=0

输出格式:
对于每组数据,第一行输出模式串最多出现的次数,接下去若干行每行输出一个出现次数最多的模式串,按输入顺序排列。

就是比普通的AC自动机多了一个记录是第几个字符串的数组eend

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<string>
using namespace std;
const int maxn=1000010;
int trie[maxn][26];
int fail[maxn];
int sum[maxn];
char str1[200][100];
char str2[maxn];
int tot=0;
int eend[maxn];
int ans[200];

void init(void)
{
    memset(trie,0,sizeof(trie));
    memset(fail,0,sizeof(fail));
    memset(sum,0,sizeof(sum));
    memset(eend,0,sizeof(eend));
    memset(ans,0,sizeof(ans));
    tot=0;
}

void _insert(int a)
{
    int p=0,k,len=strlen(str1[a]);
    for(int i=0;i<len;i++)
    {
        k=str1[a][i]-'a';
        if(!trie[p][k]) trie[p][k]=++tot;
        p=trie[p][k];
    }
    sum[p]=1;
    eend[p]=a;
}

void getfail(void)
{
    queue<int>q;
    for(int i=0;i<26;i++)
    {
        if(trie[0][i])
        {
            fail[trie[0][i]]=0;
            q.push(trie[0][i]);
        }

    }

    while(q.size())
    {
        int p;
        int now=q.front();
        q.pop();
        for(int i=0;i<26;i++)
        {
            p=trie[now][i];
            if(p)
                fail[p]=trie[fail[now]][i],q.push(p);
            else trie[now][i]=trie[fail[now]][i];

        }
    }
}

void _search(void)
{
    int p=0,k,len=strlen(str2);

    for(int i=0;i<len;i++)
    {
        k=str2[i]-'a';
        p=trie[p][k];
        for(int j=p;j;j=fail[j])
            ans[eend[j]]+=sum[j];
    }
    return ;
}


int main(void)
{


        int n;
        while(scanf("%d",&n),n)
        {
            init();
            for(int i=1;i<=n;i++)
            {
                scanf("%s",str1[i]);
                _insert(i);
            }

            getfail();

            scanf("%s",str2);

            _search();

            int maxx=-1;
            for(int i=1;i<=n;i++)
                maxx=max(maxx,ans[i]);
            printf("%d\n",maxx);
            for(int i=1;i<=n;i++)
            {
                if(ans[i]==maxx)
                printf("%s\n",str1[i]);
            }

        }
        return 0;

}

若是有重复的,再开个mp数组就好了。
如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<string>
using namespace std;
const int maxn=2000010;
int trie[maxn][26];
int fail[maxn];
int sum[maxn];
char str1[maxn];
char str2[maxn];
int tot=0;
int eend[maxn];
int ans[maxn];
int mp[maxn];

void init(int n)
{
    memset(trie,0,sizeof(trie));
    memset(fail,0,sizeof(fail));
    memset(sum,0,sizeof(sum));
    memset(eend,0,sizeof(eend));
    memset(ans,0,sizeof(ans));
    for(int i=1;i<=n;i++)
        mp[i]=i;
    tot=0;
}

void _insert(int a)
{
    int p=0,k,len=strlen(str1);
    for(int i=0;i<len;i++)
    {
        k=str1[i]-'a';
        if(!trie[p][k]) trie[p][k]=++tot;
        p=trie[p][k];
    }
    sum[p]=1;
    if(!eend[p])eend[p]=a;
    else mp[a]=eend[p];
}

void getfail(void)
{
    queue<int>q;
    for(int i=0;i<26;i++)
    {
        if(trie[0][i])
        {
            fail[trie[0][i]]=0;
            q.push(trie[0][i]);
        }

    }

    while(q.size())
    {
        int p;
        int now=q.front();
        q.pop();
        for(int i=0;i<26;i++)
        {
            p=trie[now][i];
            if(p)
                fail[p]=trie[fail[now]][i],q.push(p);
            else trie[now][i]=trie[fail[now]][i];

        }
    }
}

void _search(void)
{
    int p=0,k,len=strlen(str2);

    for(int i=0;i<len;i++)
    {
        k=str2[i]-'a';
        p=trie[p][k];
        for(int j=p;j;j=fail[j])
            ans[eend[j]]+=sum[j];
    }
    return ;
}


int main(void)
{


        int n;
        scanf("%d",&n);

        init(n);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",str1);
            _insert(i);
        }


        getfail();

        scanf("%s",str2);

        _search();

        for(int i=1;i<=n;i++)
            printf("%d\n",ans[mp[i]]);


        return 0;

}

三、P5357 【模板】AC自动机(二次加强版)
这题数据注定了不能正常AC自动机做(不能像上一题一样),因为O2都T。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<string>
using namespace std;
const int maxn=2000010;
int trie[maxn][26];
int fail[maxn];
int sum[maxn];
char str1[maxn];
char str2[maxn];
int tot=0;
int head[maxn],ver[maxn],nt[maxn];
int cnt=0;
int d[maxn];

void init(int n)
{
    memset(trie,0,sizeof(trie));
    memset(fail,0,sizeof(fail));
    memset(sum,0,sizeof(sum));
    memset(d,0,sizeof(d));
    memset(head,0,sizeof(head));
    tot=0;
    cnt=0;
}

void _insert(int a)
{
    int p=0,k,len=strlen(str1);
    for(int i=0;i<len;i++)
    {
        k=str1[i]-'a';
        if(!trie[p][k]) trie[p][k]=++tot;
        p=trie[p][k];
    }
    sum[a]=p;

}

void getfail(void)
{
    queue<int>q;
    for(int i=0;i<26;i++)
    {
        if(trie[0][i])
        {
            fail[trie[0][i]]=0;
            q.push(trie[0][i]);
        }

    }

    while(q.size())
    {
        int p;
        int now=q.front();
        q.pop();
        for(int i=0;i<26;i++)
        {
            p=trie[now][i];
            if(p)
                fail[p]=trie[fail[now]][i],q.push(p);
            else trie[now][i]=trie[fail[now]][i];

        }
    }
}

void _search(void)
{
    int p=0,k,len=strlen(str2);

    for(int i=0;i<len;i++)
    {
        k=str2[i]-'a';
        p=trie[p][k];
        d[p]++;
    }
    return ;
}

void add(int x,int y)
{
    ver[++cnt]=y,nt[cnt]=head[x],head[x]=cnt;
}

void DFS(int x)
{
    for(int i=head[x];i;i=nt[i])
    {
        DFS(ver[i]);
        d[x]+=d[ver[i]];
    }
    return ;
}

int main(void)
{


        int n;
        scanf("%d",&n);

        init(n);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",str1);
            _insert(i);
        }


        getfail();
        for(int i=1;i<=tot;i++)
            add(fail[i],i);

        scanf("%s",str2);
        _search();


        DFS(0);
        for(int i=1;i<=n;i++)
            printf("%d\n",d[sum[i]]);


        return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值