HDU 2222 AC自动机入门题

之前还是看了这个算法的,但是因为有另外的事情,时间也比较仓促,所以学的不是很明白,可以说只是AC自动机这个算法是用tire树合KMP作为基础解决多模式串匹配的问题的,但是通过在北大的学习,对这个算法又有了比较好的理解和见解,终于可以写一写这方面的专题了,毕竟是刚开始刷AC自动机的题,所以上来还是从最基础的开始写起。。。纪念一下,又对一种算法有了理解

 

这个算法总共分为三步来实现:

第一步就是把那些模式串建成tire树,这个还是相对很简单的,没什么难度

第二步就是构造失配指针,构造失败指针的过程概括起来就一句话:设这个节点上的字母为C,沿着它父亲节点的失败指针走,直到走到一个节点,它的子结点中也有字母为C的节点。然后把当前节点的失败指针指向那个字母也为C的儿子。如果一直走到了root都没找到,那就把失败指针指向root。具体操作起来只需要:先把root加入队列(root的失败指针指向自己或者NULL),这以后我们每处理一个点,就把它的所有儿子加入队列。

 

观察构造失败指针的流程:对照图来看,首先root的fail指针指向NULL,然后root入队,进入循环。从队列中弹出root,root节点与s,h节点相连,因为它们是第一层的字符,肯定没有比它层数更小的共同前后缀,所以把这2个节点的失败指针指向root,并且先后进入队列,失败指针的指向对应图中的(1),(2)两条虚线;从队列中先弹出h(右边那个),h所连的只有e结点,所以接下来扫描指针指向e节点的父节点h节点的fail指针指向的节点,也就是root,root->next['e']==NULL,并且root->fail==NULL,说明匹配序列为空,则把节点e的fail指针指向root,对应图中的(3),然后节点e进入队列;从队列中弹出s,s节点与a,h(左边那个)相连,先遍历到a节点,扫描指针指向a节点的父节点s节点的fail指针指向的节点,也就是root,root->next['a']==NULL,并且root->fail==NULL,说明匹配序列为空,则把节点a的fail指针指向root,对应图中的(4),然后节点a进入队列。接着遍历到h节点,扫描指针指向h节点的父节点s节点的fail指针指向的节点,也就是root,root->next['h']!=NULL,所以把节点h的fail指针指向右边那个h,对应图中的(5),然后节点h进入队列...由此类推,最终失配指针如图所示。

 

第三步就是进行多模式匹配了,我们可以在AC自动机上查找模式串中出现过哪些单词。匹配过程分两种情况:(1)当前字符匹配,表示从当前节点沿着树边有一条路径可以到达目标字符,此时只需沿该路径走向下一个节点继续匹配即可,目标字符串指针移向下个字符继续匹配;(2)当前字符不匹配,则去当前节点失败指针所指向的字符继续匹配,匹配过程随着指针指向root结束。重复这2个过程中的任意一个,直到模式串走到结尾为止。

 

AC自动机这个算法的时间复杂度 :假设有N个模式串,平均长度为L;文章长度为M。 建立Trie树:O(N*L) 建立fail指针:O(N*L) 模式匹配:O(M*L) 所以,大体的复杂度为:O( (N+M)*L )。

   

一道简单的水题,可以做为模板来使用的!

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=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 <cstring>
#include <queue>
using namespace std;
const int maxn = 1e6 + 100 ;

char s[maxn] ;

struct Node{
    int cnt ;
    Node *next[26] ;
    Node *fail ;
    Node (){
        memset(next, NULL, sizeof(next));
        cnt = 0;
        fail = NULL ;
    }
};

Node *root ;

void insert (){
    Node *p = root ;
    for (int i = 0; s[i]; i++){
        int id = s[i] - 'a' ;
        if (p -> next[id] == NULL){
            p -> next[id] = new Node() ;
        }
        p = p -> next[id] ;
    }
    p -> cnt++;
}

void Get_fail (){
    Node *p = root, *son, *tmp ;
    queue <Node *> que ;
    que.push(p) ;
    while (!que.empty()){
        tmp = que.front();
        que.pop() ;
        for (int i = 0; i < 26; i++){
            son = tmp -> next[i] ;
            if (son != NULL){
                if (tmp == root) {
                    son -> fail = root ;
                }
                else {
                    p = tmp -> fail ;
                    while (p){
                        if (p -> next[i]){
                            son -> fail = p -> next[i] ;
                            break ;
                        }
                        p = p -> fail ;
                    }
                    if (!p) son -> fail = root ;
                }
                que.push(son) ;
            }
        }
    }
}

void query (){
    int cnt = 0;
    Node *p = root ;
    for (int i = 0; s[i]; i++){
        int id = s[i] - 'a' ;
        while (!p -> next[id] && p != root) p = p -> fail ; // 沿着失配指针继续查找
        p = p -> next[id] ;
        if (!p) p = root ;
        Node *temp = p;
        while (temp != root){
            if (temp -> cnt >= 0){
                cnt += temp -> cnt ;
                temp -> cnt = -1;
            }
            else break ;
            temp = temp -> fail ;
        }
    }
    printf("%d\n", cnt) ;
}

int main (){
    int T, n;
    scanf("%d", &T);
    while (T--){
        root = new Node() ;
        scanf("%d", &n) ;
        getchar() ;
        for (int i = 0; i < n; i++){
            gets(s) ;
            insert() ;
        }

        Get_fail();
        gets(s) ;
        query() ;
    }
    return 0;
}

毕竟刚开始学习,还需要继续多写一写这方面的题来巩固一下自己。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值