HDU 2896 病毒侵袭 AC自动机题解

本题是在text里面查找key word的增强版,因为这里有多个text。

那么就不可以简单把Trie的叶子标志记录修改成-1进行加速了,可以使用其他技术,我直接使用个vis数组记录已经访问过的节点,达到加速效果,速度还算挺快的。

不过看discuss里面有人直接使用Trie,做出了140ms的速度,而且他的程序严格来说并不正确,可见本题的数据很水啊。Trie的时间效率肯定比AC自动机低,但是在数据很水的特殊情况下,Trie的速度也可以很快的。

注意两个细节:

1 病毒也需要安装顺序输出,不小心就遗漏了。这里害我错了几次。

2 还有就是题目的字符不确定是多少,使用33-94范围的字符测试答案正确的。

#include <stdio.h>
#include <string.h>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
const int MAX_N = 501;
const int MAX_WORD = 201;
const int MAX_M = 1001;
const int MAX_TXT = 10001;
const int MAX_V = 3;
const int ARR_SIZE = 94;
char virus[MAX_WORD], web[MAX_TXT];
int webNum[MAX_V];
int M, N;

inline int getIndex(char ch) { return ch - 33; }

struct Node
{
    int n, num, id;
    Node *fail;
    Node *arr[ARR_SIZE];
};

void clearNode(Node *rt)
{
    rt->n = 0;
    rt->num = 0;
    rt->id = 0;
    rt->fail = NULL;
    for (int i = 0; i < ARR_SIZE; i++)
    {
        rt->arr[i] = NULL;
    }
}

Node gPool[MAX_N*MAX_WORD], *Trie;
int gPoolID;
bool vis[MAX_N*MAX_WORD];    //怎么老是忘记使用visited技术的?

void insertTrie(char *virus, int num)
{
    Node *pCrawl = Trie;
    for (; *virus; virus++)
    {
        int id = getIndex(*virus);
        if (!pCrawl->arr[id])
        {
            pCrawl->arr[id] = &gPool[gPoolID++];
            clearNode(pCrawl->arr[id]);
            pCrawl->arr[id]->id = gPoolID-1;
        }
        pCrawl = pCrawl->arr[id];
    }
    pCrawl->n++;
    pCrawl->num = num;
}

void buildFail()
{
    queue<Node *> qu;
    qu.push(Trie);
    while (!qu.empty())
    {
        Node *p = qu.front(); qu.pop();
        for (int i = 0; i < ARR_SIZE; i++)
        {
            if (!p->arr[i]) continue;
            p->arr[i]->fail = Trie;
            Node *fail = p->fail;
            while (fail)
            {
                if (fail->arr[i])
                {
                    p->arr[i]->fail = fail->arr[i];
                    break;
                }
                fail = fail->fail;
            }
            qu.push(p->arr[i]);
        }
    }
}

int searchVirus(char *web)
{
    int n = 0;
    Node *pCrawl = Trie;
    memset(vis, 0, sizeof(bool)*(gPoolID+1));
    for (; *web; web++)
    {
        int i = getIndex(*web);
        while (!pCrawl->arr[i] && pCrawl != Trie) pCrawl = pCrawl->fail;
        if (pCrawl->arr[i])
        {
            pCrawl = pCrawl->arr[i];
            Node *p = pCrawl;
            while (p && !vis[p->id])//使用vis比直接检查数组值快捷方便
            {
                if (p->n) webNum[n++] = p->num;
                vis[p->id] = true;
                p = p->fail;
            }
            if (n == MAX_V) break;    //At most MAX_V virus one web
        }
    }
    return n;
}

int main()
{
    Trie = &gPool[0];
    while (scanf("%d", &N) != EOF)
    {
        gPoolID = 1;
        clearNode(Trie);
        getchar();
        for (int i = 1; i <= N; i++)
        {
            gets(virus);
            insertTrie(virus, i);
        }
        buildFail();

        scanf("%d", &M);
        getchar();
        int cnt = 0;
        for (int i = 1; i <= M; i++)
        {
            gets(web);
            int n = searchVirus(web);
            if (n)
            {
                cnt++;
                printf("web %d:", i);

                sort(webNum, webNum + n);//Watch out: order!
                for (int j = 0; j < n; j++)
                {
                    printf(" %d", webNum[j]);
                }
                putchar('\n');
            }
        }
        printf("total: %d\n", cnt);
    }
    return 0;
}

HDU 2896 病毒侵袭 AC自动机题解,布布扣,bubuko.com

时间: 2024-08-01 10:32:30

HDU 2896 病毒侵袭 AC自动机题解的相关文章

hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)

病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 19465    Accepted Submission(s): 4814 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事儿

[hdu 2896] 病毒侵袭 [ac自动机][病毒特征码匹配]

病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 20728    Accepted Submission(s): 5058 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事

HDU 2896 病毒侵袭 (AC自动机模板)

病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13063    Accepted Submission(s): 3378 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事

HDU 2896 病毒侵袭(AC自动机模版题)

AC自动模版题,中文题目就不叙述题意了啊. AC自动主要是构造出字典树之后找到fail指针的跳转,类似于KMP里面的next数组的跳转啊,注意这里是多模式跳转.意思就是这个串跳到下一个串的什么位置啊. 先帖一下,做多了再一起总结吧. 病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11347    Accepted Submi

HDU 2896 病毒侵袭(AC自动机)

解题思路: 对AC自动机算法的理解和应用,要注意的是,题目中的字符是128个ASCII码,而不是常用的26个英文字符,因此子节点的数目为128. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <cstring> #include <vector> #include &

HDU 2896 病毒侵袭

二次联通门 : HDU 2896 病毒侵袭 /* HDU 2896 病毒侵袭 AC自动机 此题太坑啦... 用G++交会无限MLE 行尾不输出回车会无限WA.. 写的程序没错 却交了N遍.. */ #include <cstring> #include <cstdio> #include <queue> #define Max 100090 void read (int &now) { now = 0; register char word = getchar

HDU 2896 病毒侵袭 (AC自动机)

题目链接:病毒侵袭 解析:利用end数组记录各病毒的编号,然后统计即可.注意此时sigma_size为128(0~127的ascii码可见字符). AC代码: #include <bits/stdc++.h> using namespace std; const int maxn = 502; const int max_word = 202; const int max_text = 10002; const int sigma_size = 128; struct Trie{ int ne

HDU - 2896 病毒侵袭 (AC自动机,last优化)

题目链接:HDU - 2896 题意:给你n个模式串,对应每个模式串由编号,给出m个文本串,然后你要输出对应所匹配出模式串序号,以及有多少个文本串中有模式串 思路:比起比较基本统计个数,这里我们可以用set或者map来统计模式串序号 这里总结一下新学习的last优化. 参考博客:(1) last相当于一个超级fail指针: 因为我们只有到根节点时才会重新匹配一个字母,所以我们此时直接记录一个last ,直接结束当前匹配过程.直接省去原 Fail 指针到可以匹配的节点之间的距离. 同时结合路径压缩

HDU 2896 病毒侵袭(AC自动机/模板题)

传送门 病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)            Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 63941             Accepted Submission(s): 21264 Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一