AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35655    Accepted Submission(s): 11496

Problem Description

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



Mean:

给你n个单词,再给你一篇文章,让你统计有多少个单词在文章中出现过。

analyse:

裸的AC自动机,模板题。

Time complexity:o(n)+o(ml)    n个模式串长度均不超过m,文本串长度为L

Source code:

// Memory   Time
// 1347K     0MS
// by : Snarl_jsb
// 2014-09-29-20.14
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 10010
#define LL long long
using namespace std;

namespace ac_auto
{
    char str[1000005];
    struct node
    {
        node *next[26];
        node *fail;
        int count;
        node()
        {
            for(int i = 0; i < 26; i++)
                next[i] = NULL;
            count = 0;
            fail = NULL;
        }
    }*q[50*N];
    node *root;
    int head, tail;

    void Insert(char *str)      //      插入单词
    {
        node *p = root;
        int i = 0, index;
        while(str[i]) {
            index = str[i] - ‘a‘;
            if(p->next[index] == NULL)
                p->next[index] = new node();
            p = p->next[index];
            i++;
        }
        p->count++;
    }
    void build_ac_automation(node *root)        //      bfs建立fail指针
    {
        root->fail = NULL;
        q[tail++] = root;
        while(head < tail) {
            node *temp = q[head++];
            node *p = NULL;
            for(int i = 0; i < 26; i++) {
                if(temp->next[i] != NULL) {
                    if(temp == root) temp->next[i]->fail = root;
                    else {
                        p = temp->fail;
                        while(p != NULL) {
                            if(p->next[i] != NULL) {
                                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 Query(node *root)       //  匹配 + 统计
    {
        int i = 0, cnt = 0, index;
        node *p = root;
        while(str[i]) {
            index = str[i] - ‘a‘;
            while(p->next[index] == NULL && p != root) p = p->fail;
            p = p->next[index];
            if(p == NULL) p = root;
            node *temp = p;
            while(temp != root && temp->count != -1) {
                cnt += temp->count;
                temp->count = -1;
                temp = temp->fail;
            }
            i++;
        }
        return cnt;
    }
}
using namespace ac_auto;

int main()
{
    int T, n;
    scanf("%d",&T);
    while(T--)
    {
        head = tail = 0;    //  清零
        root = new node();      //  申请新的root结点
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s", str);
            Insert(str);    //  插入单词
        }
        build_ac_automation(root);      //  建树
        scanf("%s",str);
        printf("%d\n", Query(root));        //      查找+统计
    }
    return 0;
}

  

时间: 2024-12-13 18:20:42

AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222的相关文章

AC自动机——多模式串匹配的算法思想

标准KMP算法用于单一模式串的匹配,即在母串中寻求一个模式串的匹配,但是现在又存在这样的一个问题,如果同时给出多个模式串,要求找到这一系列模式串在母串存在的匹配个数,我们应该如何处理呢? 基于KMP算法,我们能够想到的一个朴素算法就是,枚举这多个模式串,然后进行多次KMP算法,这个过程中完成计数,假设这里有n个模式串,那么整个算法的复杂度大约是O(n*m),m是母串的长度,这里的时间复杂度是粗略估计,没有计算辅助数组的时间(KMP中的next数组),但是这种复杂度还是太高,没有做到KMP算法中“

4.【ac自动机】模式串匹配

ANSI编码的中英文16叉模式串匹配自动机 1.构造模式串树 void insert(char* s, in* trie) { long u = 1, len = strlen(s);//每来一个模式串 for (long i = 0; i < len * 2; i++) { if (i % 2 == 0) { uint8_t vv = (uint8_t)s[i / 2]; uint8_t v = vv >> 4; if (!trie[u].son[v]) trie[u].son[v]

AC自动机 - 多模式串的匹配运用 --- HDU 2896

病毒侵袭 Problem's Link:http://acm.hdu.edu.cn/showproblem.php?pid=2896 Mean: 中文题,不解释. analyse: AC自动机的运用,多模式串匹配.就是有几个细节要注意,在这些细节上卡了半天了. 1)输出的网站编号和最终的病毒网站数不是一样的: 2)next指针要设128,不然会爆栈: 3)同理,char转换为int时,base要设为31: Time complexity:o(n)+o(ml)  Source code: // M

【暖*墟】 #AC自动机# 多模式串的匹配运用

一.构建步骤 1.将所有模式串构建成 Trie 树 2.对 Trie 上所有节点构建前缀指针(类似kmp中的next数组) 3.利用前缀指针对主串进行匹配 AC自动机关键点一:trie字典树的构建过程 字典树的构建过程是这样的,当要插入许多单词的时候,我们要从前往后遍历整个字符串, 当我们发现当前要插入的字符其节点再先前已经建成,我们直接去考虑下一个字符即可, 当我们发现当前要插入的字符没有再其前一个字符所形成的树下没有自己的节点, 我们就要创建一个新节点来表示这个字符,接下往下遍历其他的字符.

AC自动机 - AC自动机 - 多模式串的匹配运用 --- HDU 3065

病毒侵袭持续中 Problem's Link:http://acm.hdu.edu.cn/showproblem.php?pid=3065 Mean: 中文题,不解释. analyse: AC自动机的运用.这一题需要将模式串都存储下来,还有就是base的取值一定要弄清楚,由于这题的模式串都是大写字母所以我们可以通过剪枝来加速. Time complexity:o(n)+o(ml)  Source code: // Memory Time // 1347K 0MS // by : Snarl_js

HDU 5880 Family View (AC自动机)

Family View Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description Steam is a digital distribution platform developed by Valve Corporation offering digital rights management (DRM), multiplayer gaming and socia

高效处理字符串!——AC自动机

AC自动机 这两天进军AC自动机算法,越做越觉得这种算法的灵活与高效,接下来对这阵子的学习做个总结. AC自动机,当然它最主要的作用是自动帮你AC题目多模式串的匹配,也就是字典树trie和kmp的结合,再深入讲就是把kmp中失配时跳转的思想运用到trie上! 1.AC自动机构建 对于构建,基本上都是模板,先建trie,再BFS这颗trie从而构建出最重要的fail指针,即失配跳转指针(口头表达),fail指针指向的是当前状态的最长后继.         而一般我们为了加快速度,每个节点还会构建类

数据结构14——AC自动机

一.相关介绍 知识要求 字典树Trie KMP算法 AC自动机 多模式串的字符匹配算法(KMP是单模式串的字符匹配算法) 单模式串问题&多模式串问题 单模就是给你一个模式串,问你这个模式串是否在主串中出现过,这个问题可以用kmp算法高效完成: 多模就是给你多个模式串,问你有多少个模式串在这个主串中出现过. 若我们暴力地用每一个模式串对主串做kmp,这样虽然理论上可行,但是时间复杂度非常之高.而AC自动机算法就能高效地处理这种多模式串问题. 二.算法实现 [打基础] 失配指针fail 每个节点都有

AC自动机

AC自动机 直接学AC自动机比较难理解,强烈建议先学完KMP和字典树并进行一定的练习后,对于失配指针和字典树构造有一定理解后再来学AC自动机的内容.有关AC自动机的详细介绍可见刘汝佳的<算法竞赛入门经典训练指南>P214. 给你一个字典(包含n个不重复的单词),然后给你一串连续的字符串文本(长为len),问你该文本里面的哪些位置正好出现了字典中的某一个或某几个单词?输出这些位置以及出现的单词. 这个问题可以用n个单词的n次KMP算法来做(效率为O(n*len*单词平均长度)),也可以用1个字典