AC自动机小结 (HDU 2222,HDU 2896,HDU 3065)



学习了两天ac自动机,有所收获,做一个小结;

目前学的还比较浅,知道ac自动机的用处之一是可以快速求出在一个字符串中多个模式串出现的次数,即多模式串匹配问题,相较KMP则是但模式串匹配;

自我觉得ac自动机最难得理解的是fail指针的构造,感觉非得自己手动在草稿纸上模拟一边才懂,就像KMP里面的next数组,其次是字典树不会的先得把字典树学了再来看ac自动机,字典树还算好理解;


推荐学习博客:http://blog.csdn.net/niushuai666/article/details/7002823


做了3个入门题,讲一下:

HDU 2222  http://acm.hdu.edu.cn/showproblem.php?pid=2222

这题就是最基础的模板题,求在最后给出的字符串中模式串出现了几种;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<stack>
#define inf 0x7fffffff
#define maxn 50005
using namespace std;
struct node
{
    int sum;
    node *fail;
    node *next[26];
}*root,*newnode;
int n,cnt;
struct N
{
    node *p;
};
queue<N> que;
char s[1000005],str[70];
node *q[1000005];
int head,tail;
void Inseart(char *s)
{
    node *p=root;
    for(int i=0;s[i]!=NULL;i++)
    {
        int x=s[i]-'a';
        if(p->next[x]==NULL)
        {
            newnode=new node;
            for(int i=0;i<26;i++) newnode->next[i]=0;
            newnode->sum=0; newnode->fail=0;
            p->next[x]=newnode;
        }
        p=p->next[x];
    }
    p->sum++;
}
void build_fail()
{
    head = 0;
    tail = 1;
    q[head] = root;
    node *p;
    node *temp;
    while(head < tail)
    {
        temp = q[head++];
        for(int i = 0; i <= 25; i++)
        {
            if(temp->next[i])
            {
                if(temp == root)
                    temp->next[i]->fail = root;
                else
                {
                    p = temp->fail;
                    while(p)
                    {
                        if(p->next[i])
                        {
                            temp->next[i]->fail = p->next[i];
                            break;
                        }
                        p = p->fail;
                    }
                    if(p == NULL) temp->next[i]->fail = root;
                }
                q[tail++] = temp->next[i];
            }
        }
    }
}

void ac_automation(char *s)
{
    node *p=root;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int x=s[i]-'a';
        while(!p->next[x]&&p!=root) p=p->fail;
        p=p->next[x];
        if(!p) p=root;
        node *tmp=p;
        while(tmp!=root)
        {
            if(tmp->sum>=0)
            {
                cnt+=tmp->sum;
                tmp->sum=-1;
            }
            else break;
            tmp=tmp->fail;
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        root=new node;
        for(int i=0;i<26;i++) root->next[i]=0;
        root->fail=0; root->sum=0;
        for(int i=1;i<=n;i++)
            scanf("%s",str),Inseart(str);
        build_fail();
        scanf("%s",s);
        cnt=0;
        ac_automation(s);
        printf("%d\n",cnt);
    }
    return 0;
}


HDU 2896  http://acm.hdu.edu.cn/showproblem.php?pid=2896

题意自己看吧,这题需要注意的是用指针写教G++会MLE,程序没问题交c++会过,我也不知道为什么,自己写了一个delete函数感觉没什么用。。。这题格式要注意一下,容易PE

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<stack>
#include<functional>
using namespace std;
int n,m;
char str[205],s[10005];
struct node
{
    node *next[130];
    node *fail;
    int sum;
    int id;
    void init()
    {
        for(int i=0;i<130;i++)
            next[i]=0;
        fail=0;
        sum=0;
        id=0;
    }
}*root,*newnode,*tmp,*fa;
queue<node*> que;
priority_queue<int,vector<int>, greater<int> > ans;//记录每次的网站出现的病毒编号
int cnt;
bool vis[505];//标记病毒是否被记录过
void Insert(char *s,int id)
{
    tmp=root;
    for(int i=0;s[i];i++)
    {
        int x=(int)s[i];
        if(!tmp->next[x])
        {
            newnode=new node;
            newnode->init();
            tmp->next[x]=newnode;
        }
        tmp=tmp->next[x];
    }
    tmp->id=id;
}
void build_fail_point()
{
    que.push(root);
    while(que.size())
    {
        fa=que.front();
        que.pop();
        for(int i=0;i<130;i++)
        {
            if(fa->next[i])
            {
                if(fa==root)
                    fa->next[i]->fail=root;
                else
                {
                    tmp=fa->fail;
                    while(tmp!=root&&!tmp->next[i]) tmp=tmp->fail;
                    tmp=tmp->next[i];
                    if(!tmp) fa->next[i]->fail=root;
                    else fa->next[i]->fail=tmp;
                }
                que.push(fa->next[i]);
            }
        }
    }
}

void ac_automation(char *s,int id)
{
    fa=root;
    memset(vis,false,sizeof(vis));
    for(int i=0;s[i];i++)
    {
        int x=(int)s[i];
        while(fa!=root&&!fa->next[x]) fa=fa->fail;
        fa=fa->next[x];
        if(!fa) fa=root;
        tmp=fa;
        while(tmp!=root)
        {
            if(tmp->id&&!vis[tmp->id])
            {
                ans.push(tmp->id);//将当前病毒入队
                vis[tmp->id]=true;
                tmp=tmp->fail;
            }
            else break;
        }
    }
    if(ans.size())
    {
        cnt++;//出现病毒的总网站数
        printf("web %d:",id);
        while(ans.size())
        {
            printf(" %d",ans.top());
            ans.pop();
        }
        printf("\n");
    }
}
void Delete(node *root)//释放申请的内存
{
    if(!root) return;
    for(int i=0;i<130;i++)
    {
        if(root->next[i])
            Delete(root->next[i]);
    }
    delete root;
    return ;
}

int main()
{
    while(~scanf("%d",&n))
    {
        root=new node;
        root->init();
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%s",str);
            Insert(str,i);
        }
        build_fail_point();
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%s",s);
            ac_automation(s,i);
        }
        printf("total: %d\n",cnt);
        Delete(root);
    }
    return 0;
}

HDU 3065  http://acm.hdu.edu.cn/showproblem.php?pid=3065

这题跟上面那题差不多,但是这题求的是每个模式串出现的次数,比如  AA在AAA中出现的次数是2次,那么我们在匹配的时候一个串出现过不标记就行了,前两份代码在一个串出现一次之后就标记了为的就是不重复计数,但这题就是问出现次数,所以不标记就行了


#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
char str[1005][55],s[2000005];
int n,ans[1005];
struct node
{
    node *next[26];
    node *fail;
    int id;//记录是第几个串
    node ()//初始化
    {
        memset(next,NULL,sizeof(next));
        fail=NULL;
        id=0;
    }
}*p,*root,*tmp,*newnode;

queue<node*> que;

void Insert(char *s,int id)
{
    p=root;
    for(int i=0;s[i];i++)
    {
        int x=s[i]-'A';
        if(!p->next[x])
            p->next[x]=new node;
        p=p->next[x];
    }
    p->id=id;;
}
void build_fail_point()
{
    que.push(root);
    while(que.size())
    {
        p=que.front();
        que.pop();
        for(int i=0;i<26;i++)
        {
            if(p->next[i])
            {
                if(p==root)
                    p->next[i]->fail=root;
                else
                {
                    tmp=p->fail;
                    while(tmp!=root&&!tmp->next[i]) tmp=tmp->fail;
                    tmp=tmp->next[i];
                    if(!tmp) p->next[i]->fail=root;
                    else p->next[i]->fail=tmp;
                }
                que.push(p->next[i]);
            }
        }
    }
}
void ac_automation(char *s)
{
    p=root;
    for(int i=0;s[i];i++)
    {
        int x=s[i]-'A';
        if(x<0||x>=26){//不是A~Z的字符直接continue,注意要把p指向root
            p=root;continue;
        }
        while(p!=root&&!p->next[x]) p=p->fail;
        p=p->next[x];
        if(!p) p=root;
        tmp=p;
        while(tmp!=root)
        {
            if(tmp->id)
            {
                ans[tmp->id]++;
                tmp=tmp->fail;
            }
            else break;
        }
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        root=new node;
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=n;i++)
        {
            scanf("%s",str[i]);
            Insert(str[i],i);
        }
        build_fail_point();
        scanf("%s",s);
        ac_automation(s);
        for(int i=1;i<=n;i++)
        {
            if(ans[i])
                printf("%s: %d\n",str[i],ans[i]);
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值