题意:给你几个keywords,再给你一段文章,问你keywords出现了几次。

思路:这里就要用到多模匹配算法AC自动机了,AC自动机需要KMP和字典树的知识,匹配时是在字典树上,失配我们就要用到类似KMP的失配值了,如果失配,我们就沿着失配值到某个节点开始匹配,因为是多模匹配,我们每次失配移动都会从某一keyword的某部分开始匹配,这样就节省了很多时间。

话说第一次听到AC自动机我竟天真的以为是会自动AC题目的算法...orz

参考:

AC自动机算法详解 (转载)

ac自动机最详细的讲解,让你一次学会ac自动机。

AC自动机算法及模板

代码:

#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define ll long long
const int maxn = 1000000+5;
const int maxm = 100000+5;
const int MOD = 1e7;
const int INF = 0x3f3f3f3f;
using namespace std;
struct Trie{
    Trie *next[26];
    Trie *fail; //失配值
    int sum;    //以此为单词结尾的个数
    Trie(){
        sum = 0;
        memset(next,NULL,sizeof(next));
        fail = NULL;
    }
};
Trie *root;
Trie *q[maxn];  //模拟队列
int head,tail;
void insert(char *s){
    Trie *p = root;
    for(int i = 0;s[i];i++){
        int x = s[i] - 'a';
        if(p ->next[x] == NULL){
            p ->next[x] = new Trie();
        }
        p = p ->next[x];
    }
    p ->sum++;
}
void buildFail(){   //计算失配值
    head = 0,tail = 1;
    q[head] = root;
    Trie *p,*temp;
    while(head < tail){
        temp = q[head++];
        for(int i = 0;i <= 25;i++){
            if(temp ->next[i]){
                if(temp == root){   //父节点为root,fail为root
                    temp ->next[i] ->fail = root;
                }
                else{
                    p = temp ->fail;    //查看父节点的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];
            }
        }
    }
}
int ac_automation(char *ch){
    //p为模式串指针
    int cnt = 0;
    Trie *p = root;
    int len = strlen(ch);
    for(int i = 0;i < len;i++){
        int x = ch[i] - 'a';
        while(!p ->next[x] && p != root)
            p = p ->fail;
        p = p ->next[x];    //找到后p指针指向该结点
        if(!p) p = root;    //若指针返回为空,则没有找到与之匹配的字符
        Trie *temp = p;
        while(temp != root){
            if(temp ->sum >= 0){    //判断该结点是否被访问
                cnt += temp ->sum;
                temp ->sum = -1;
            }
            else break;
            temp = temp ->fail;
        }
    }
    return cnt;
}
char word[maxn];
int main(){
    int T,n;
    char s[55];
    scanf("%d",&T);
    while(T--){
        root = new Trie();
        scanf("%d",&n);
        for(int i = 0;i < n;i++){
            scanf("%s",s);
            insert(s);
        }
        scanf("%s",word);
        buildFail();
        int cnt = ac_automation(word);
        printf("%d\n",cnt);
    }
    return 0;
}

HDU 2222 Keywords Search(AC自动机)题解的更多相关文章

  1. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

  2. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  3. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  4. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

  5. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  6. HDU 2222 Keywords Search (AC自动机)(模板题)

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  7. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  8. hdoj 2222 Keywords Search(AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...

  9. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  10. HDU 2222 Keywords Search AC自己主动机入门题

    单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...

随机推荐

  1. 设计模式之单例模式(JAVA实现)

    单例模式之自我介绍 我,单例模式(Singleton Pattern)是一个比较简单的模式,我的定义如下: Ensure a class has only one instance,and provi ...

  2. 实用的IOS应用程序框架

    实用的IOS应用程序框架 目录 概述 概述

  3. 【BZOJ2668】[cqoi2012]交换棋子 费用流

    [BZOJ2668][cqoi2012]交换棋子 Description 有一个n行m列的黑白棋盘,你每次可以交换两个相邻格子(相邻是指有公共边或公共顶点)中的棋子,最终达到目标状态.要求第i行第j列 ...

  4. Android屏幕适配和文字屏幕适配

    http://blog.sina.com.cn/s/blog_9996c67e0101euwd.html 最近在一个项目中要实现屏幕适配平板和手机等不同的型号,而蛋疼的美工给了一套图,而且这些图纸有在 ...

  5. angularJS中的ng-repeat指令!

    ng-repeat 指令: ng-repeat 指令用来遍历一个数组重复创建当前元素: <ul ng-app="myApp" ng-controller="myAp ...

  6. Sublime的使用!emmet常用快捷键梳理

    多的不说了! 示例一: !+tab 效果: <!doctype html> <html lang="en"> <head> <meta c ...

  7. vector容器建图

    #pragma comment(linker, "/STACK:1024000000,1024000000") #include"stdio.h" #inclu ...

  8. shell 文件备份脚本

    #!/bin/bash #输入参数:文件名 filename=$ #源文件目录 directory=/opt/docker/cloud-driver-training/apps #备份文件目录 bac ...

  9. angular js 上传插件 ng-file-upload 使用时注意事项

    项目框架为angular js,需要用到文件上传,百度之后先选择了angular-file-upload,githuab上API文档很全,想要具体了解,可以仔细研究一下.在这里简单回顾一下自己使用的插 ...

  10. redis cluster 集群畅谈(三) 之 水平扩容、slave自动化迁移

    上一篇http://www.cnblogs.com/qinyujie/p/9029522.html, 主要讲解 实验多master写入.读写分离.实验自动故障切换(高可用性),那么本篇我们就来聊了聊r ...