zoj 3430 Detect the Virus(AC自动机)

题目连接:zoj 3430 Detect the Virus

题目大意:给定一个编码完的串,将每一个字符对应着表的数值转换成6位二进制,然后以8为一个数值,重新形成字符

串,判断给定询问串是否含有字符集中的串。

解题思路:主要是题意,逆编码部分注意,转换完了之后,可能有字符‘\0‘,所以不能用字符串的形式储存,要用int型

的数组。注意有相同串的可能。

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

const int maxn = 512 * 100;
const int sigma_size = 256;

struct Aho_Corasick {
    int sz, g[maxn][sigma_size];
    int tag[maxn], fail[maxn], last[maxn];
    bool vis[520];
    int jump[520];

    void init();
    int idx(char ch);
    void insert(int* str, int k);
    void getFail();
    int match(int* str);
    void put(int u);
}A;

inline int change(char ch) {
    if (ch >= ‘A‘ && ch <= ‘Z‘)
        return ch - ‘A‘;
    if (ch >= ‘a‘ && ch <= ‘z‘)
        return ch - ‘a‘ + 26;
    if (ch >= ‘0‘ && ch <= ‘9‘)
        return ch - ‘0‘ + 52;
    if (ch == ‘+‘)
        return 62;
    return 63;
}

const int maxl = 5005;

char w[maxl];
int s[maxl * 2];

void input(int* s) {

    scanf("%s", w);
    s[0]=0;
    int n = strlen(w);
    while (n && w[n-1] == ‘=‘) n--;
    w[n] = 0;
    for(int i=0,len=0,x=0;w[i];++i){
        len+=6,x=(x<<6)|change(w[i]);
        if(len>=8){
            s[++s[0]]=(x>>(len-8))&0xff;
            len-=8;
        }
    } 

    /*
       for(int i = 1; i <= s[0]; i++)
       printf(" %d", s[i]);
       printf("\n");
       */
}

int N, M;

int main () {
    while (scanf("%d", &N) == 1) {
        A.init();

        for (int i = 1; i <= N; i++) {
            input(s);
            A.insert(s, i);
        }
        A.getFail();

        scanf("%d", &M);
        for (int i = 1; i <= M; i++) {
            input(s);
            printf("%d\n", A.match(s));
        }
        printf("\n");
    }
    return 0;
}

void Aho_Corasick::init() {
    sz = 1;
    tag[0] = 0;
    memset(g[0], 0, sizeof(g[0]));
}

int Aho_Corasick::idx(char ch) {
    return ch;
}

void Aho_Corasick::put(int u) {
    int p = tag[u];
    while (p && vis[p] == 0) {
        vis[p] = 1;
        p = jump[p];
    }
    if (last[u])
        put(last[u]);
}

void Aho_Corasick::insert(int* str, int k) {
    int u = 0;

    for (int i = 1; i <= str[0]; i++) {
        int v = str[i];
        while (v >= sigma_size);
        if (g[u][v] == 0) {
            tag[sz] = 0;
            memset(g[sz], 0, sizeof(g[sz]));
            g[u][v] = sz++;
        }
        u = g[u][v];
    }

    jump[k] = tag[u];
    tag[u] = k;
}

int Aho_Corasick::match(int* str) {
    memset(vis, 0, sizeof(vis));
    int u = 0;

    for (int i = 1; i <= str[0]; i++) {
        int v = str[i];
        while (v >= sigma_size);
        while (u && g[u][v] == 0)
            u = fail[u];

        u = g[u][v];

        if (tag[u])
            put(u);
        else if (last[u])
            put(last[u]);
    }

    int ret = 0;
    for (int i = 1; i <= N; i++)
        if (vis[i])
            ret++;
    return ret;
}

void Aho_Corasick::getFail() {
    queue<int> que;

    for (int i  = 0; i < sigma_size; i++) {
        int u = g[0][i];
        if (u) {
            fail[u] = last[u] = 0;
            que.push(u);
        }
    }

    while (!que.empty()) {
        int r = que.front();
        que.pop();

        for (int i = 0; i < sigma_size; i++) {
            int u = g[r][i];

            if (u == 0) {
                g[r][i] = g[fail[r]][i];
                continue;
            }

            que.push(u);
            int v = fail[r];
            while (v && g[v][i] == 0)
                v = fail[v];

            fail[u] = g[v][i];
            last[u] = tag[fail[u]] ? fail[u] : last[fail[u]];
        }
    }
}
时间: 2024-10-07 02:08:51

zoj 3430 Detect the Virus(AC自动机)的相关文章

zoj 3430 Detect the Virus(AC自动机)

Detect the Virus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his computer is extremely slow. After several hours' work, he finally found that it was a virus that made his poor computer slow and the virus was activated

ZOJ 3430 Detect the Virus (AC自动机)

题目链接:Detect the Virus 题意:n个模式串,一个文本串,问文本串包含几个模式串. 解析:解码 + AC自动机. 解码过程:先将字符串转换成ASCII 然后根据相应的ASCII 转换成二进制,每一个是6位,不够加0,然后取8位为一个字符,求得的字符串为要的字符串. PS:注意sigma_size = 256 AC代码: #include <bits/stdc++.h> using namespace std; struct Trie{ int next[520*64][256]

zoj 3430 Detect the Virus(AC自己主动机)

Detect the Virus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his computer is extremely slow. After several hours' work, he finally found that it was a virus that made his poor computer slow and the virus was activated

ZOJ3430 Detect the Virus AC自动机

题意:给你base64编码后的模式串和文本串,让你看编码之前的文本串和分别包含了多少模式串 解题思路:主要是编码还有注意分支要开256 ,然后就是裸的AC自动机 解题代码: 1 // File Name: temp.cpp 2 // Author: darkdream 3 // Created Time: 2014年09月11日 星期四 15时18分256秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8

【ZOJ】3430 Detect the Virus

动态建树MLE.模仿别人的代码模板各种原因wa后,终于AC. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 515*70 8 #define NXTN 256 9 10 bool visit[515]; 11 char str[3005]; 12 unsigned

zoj3430Detect the Virus(ac自动机)

链接 解码之后是跟普通的自动机求解一下的,只不过解码比较恶心,512=>N>=0 ,所以不能用字符串来存,需要转换成整数来做. 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8

zoj 3228 Searching the String(AC自动机)

题目连接:zoj 3228 Searching the String 题目大意:给定一个字符串,然后现在有N次询问,每次有一个type和一个子串,问说子串在字符串中出现几次,type 为0时为可重叠,为1时为不可重叠. 解题思路:不过没有type=1的限制,那么就是普通的AC自动机匹配问题,对于不可重叠问题,可以对于每个节点记录 一下上一次匹配到的pos,用当前匹配的i减掉pos看有没有超过长度,有超过即为合法匹配,否则忽略. 题目中有很多相同的子串,一开始我用jump数组用类似链表的形式记录每

Zoj 3545 Rescue the Rabbit(ac自动机+dp)

题目大意: 给出的DNA序列有一个权值,请构造一个长度为I的DNA序列使得在这段DNA序列的权值最大.如果为负数就输出噼里啪啦... 思路分析: 构造序列就是在ac自动机上走,求最大要用到dp dp[i][j][k] 表示现在构造到了长度 i .此时的我们把当前字符放在j节点,并且满足了k状态.k是一个10位的2进制状态压缩. 注意这道题上有坑就是一个序列可能有多个权值.所以不能直接赋值,需要用位或. #include <cstdio> #include <iostream> #i

ZOJ 3228 Searching the String AC自动机的不重复匹配

这个判断方法真的没想到... 对于在S中匹配M,如果M上一次的匹配位置pre与这一次的匹配位置now满足now-pre >= M.length,则加1. 这个判断太跳了233 . #include <iostream> #include<time.h> #include<stdio.h> #include<string.h> #include<stdlib.h> #include<string> #include<map&