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
by a misoperation of opening an attachment of an email.

Nobita did use an outstanding anti-virus software, however, for some strange reason, this software did not check email attachments. Now Nobita decide to detect viruses in emails by himself.

To detect an virus, a virus sample (several binary bytes) is needed. If these binary bytes can be found in the email attachment (binary data), then the attachment contains the virus.

Note that attachments (binary data) in emails are usually encoded in base64. To encode a binary stream in base64, first write the binary stream into bits. Then take 6 bits from the stream
in turn, encode these 6 bits into a base64 character according the following table:

That is, translate every 3 bytes into 4 base64 characters. If the original binary stream contains 3k + 1 bytes, where k is an integer, fill last bits using zero
when encoding and append ‘==‘ as padding. If the original binary stream contains 3k + 2 bytes, fill last bits using zero when encoding and append ‘=‘ as padding. No padding is needed when the original binary stream contains 3k bytes.

Value 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Encoding A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f
Value 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
Encoding g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 + /

For example, to encode ‘hello‘ into base64, first write ‘hello‘ as binary bits, that is: 01101000
01100101 01101100 01101100 01101111

Then, take 6 bits in turn and fill last bits as zero as padding (zero padding bits are marked in bold): 011010 000110 010101 101100 011011 000110 111100

They are 26 6 21 44 27 6 60 in decimal. Look up the table above and use corresponding characters: aGVsbG8

Since original binary data contains 1 * 3 + 2 bytes, padding is needed, append ‘=‘ and ‘hello‘ is finally encoded in base64: aGVsbG8=

Section 5.2 of RFC 1521 describes how to encode a binary stream in base64 much more detailedly:

Click here to see Section
5.2 of RFC 1521 if you have interest

Here is a piece of ANSI C code that can encode binary data in base64. It contains a function, encode (infile, outfile), to encode binary file infile in base64 and output
result to outfile.

Click here to see the reference
C code if you have interest

Input

Input contains multiple cases (about 15, of which most are small ones). The first line of each case contains an integer N (0 <= N <= 512). In the next N distinct
lines, each line contains a sample of a kind of virus, which is not empty, has not more than 64 bytes in binary and is encoded in base64. Then, the next line contains an integer M (1 <= M <= 128). In the following M lines,
each line contains the content of a file to be detected, which is not empty, has no more than 2048 bytes in binary and is encoded in base64.

There is a blank line after each case.

Output

For each case, output M lines. The ith line contains the number of kinds of virus detected in the ith file.

Output a blank line after each case.

Sample Input

3
YmFzZTY0
dmlydXM=
dDog
1
dGVzdDogdmlydXMu

1
QA==
2
QA==
ICAgICAgICA=

Sample Output

2

1
0

Hint

In the first sample case, there are three virus samples: base64, virus and t: ,
the data to be checked is test: virus., which contains the second and the third, two virus samples.

题意:给出n个编码后的模板串,然后有M次询问,每次询问输入一个编码后的文本串,问在编码前,有多少个模板串在文本串中出现过。

分析:我认为这道题的难点不在于构建AC自动机,而在于如何把编码后的字符串解码成原文。编码时先把字符串中的每一个字符转化为一个十进制整数,然后把每个整数转化为8位2进制整数,然后把这些2进制数连接起来,从头开始每次取6位,不足6位时在后边补0,然后把这6位二进制转化为一个新的十进制整数,这个新的整数就是编码后的字符的ASCII码,编码前的字符数量如果不是3的倍数,则在编码后添加‘=’,数量为字符数除以3的余数。  因为解码后的字符的ASCII值是从0~255的,所以最好不要用字符串存储解码后的内容,用int来保存较好一些。

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;

const int kind = 256; //the maximum number of letter
const int N = 50010;
struct Node
{
    int next[kind];
    int fail;
    int cnt;
    void build_node() {
        for(int i = 0; i < kind; i++)
            next[i] = 0;
        fail = 0;
        cnt = 0;
    }
} node[N];
int digit[N], code[N], Size, vis[N];
char file[N], vir[N];

int fun(char ch)  //convert char to int
{
    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;
}

void change(char *str)
{
    int i, j, len, t;
    vector<int> v;
    memset(digit, 0, sizeof(digit));
    for(len = strlen(str); str[len-1] == '='; len--) ;
    str[len] = '\0';
    for(i = 0; i < len; i++)
        v.push_back(fun(str[i]));
    for(i = 0; i < v.size(); i++) {
        for(j = 6 * (i + 1) - 1; j >= 6 * i; j--) {
            if(v[i]&1)
                digit[j] = 1;
            v[i] >>= 1;
        }
    }
    int k = v.size() * 6 / 8; //the number of letter before encode
    for(i = 0; i < k; i++) {
        for(t = 0, j = 8 * i; j < 8 * (i + 1); j++)
            t = (t << 1) + digit[j];
        code[i] = t;
    }
    code[i] = -1;
}

void Insert()
{
    int cur, index, i;
    for(cur = i = 0; code[i] >= 0; i++) {
        index = code[i];
        if(node[cur].next[index] == 0) {
            node[++Size].build_node();
            node[cur].next[index] = Size;
        }
        cur = node[cur].next[index];
    }
    node[cur].cnt++;
}
void build_ac_automation(int root)
{
    queue<int> q;
    q.push(root);
    while(!q.empty()) {
        int cur = q.front(); q.pop();
        for(int i = 0; i < kind; i++) {
            if(node[cur].next[i]) {
                int p = node[cur].next[i];
                if(cur)
                    node[p].fail = node[node[cur].fail].next[i];
                q.push(p);
            }
            else
                node[cur].next[i] = node[node[cur].fail].next[i];
        }
    }
}
int Query()
{
    int ans = 0;
    for(int i = 0, cur = 0; code[i] >= 0; i++) {
        int index = code[i];
        cur = node[cur].next[index];
        for(int j = cur; j; j = node[j].fail) {
            if(!vis[j]) {
                ans += node[j].cnt;
                vis[j] = 1;
            }
        }
    }
    return ans;
}

int main()
{
    int n, m;
    while(~scanf("%d",&n)) {
        node[0].build_node();
        Size = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%s",vir);
            change(vir);
            Insert();
        }
        build_ac_automation(0);
        scanf("%d",&m);
        while(m--) {
            memset(vis, 0, sizeof(vis));
            scanf("%s", file);
            change(file);
            printf("%d\n", Query());
        }
        printf("\n");
    }
    return 0;
}

zoj 3430 Detect the Virus(AC自动机),布布扣,bubuko.com

时间: 2024-08-09 19:50:28

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

zoj 3430 Detect the Virus(AC自动机)

题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每一个字符对应着表的数值转换成6位二进制,然后以8为一个数值,重新形成字符 串,判断给定询问串是否含有字符集中的串. 解题思路:主要是题意,逆编码部分注意,转换完了之后,可能有字符'\0',所以不能用字符串的形式储存,要用int型 的数组.注意有相同串的可能. #include <cstdio> #include <cstring> #include <queue> #incl

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&