ZOJ 3430 Detect the Virus ac 自动机

Detect the Virus(ac 自动机)
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Submit  Status  Practice  ZOJ 3430
Appoint description:  System Crawler  (2016-01-17)

Description

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 fileinfile 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: base64virus and t: , the data to be checked is test: virus., which contains the second and the third, two virus samples.



原本以为是AC自动机的水题,没想到竟然卡在查询上了,看来以后不能随便套模板了。


题意:给出几个字符串,和一个主串,问这些字符串在主串中出现的个数

思路:标准的AC自动机,只不过这道题需要将字符串做处理,也是一种很麻烦的处理,我数学不好,只能用最笨的方法,先把每一个字符转换成数字,再转换成二进制,不够的0添上,再将二进制转换为十进制,注意,这里的十进制范围是0-255,不能再用字符串存了,因为有'\0'的存在,所以需要使用整形数组存储,AC自动机也要做相应变化。


代码:


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
#define MAX 60000
using namespace std;

struct Trie//封装的AC自动机
{
    int Next[MAX][256],fail[MAX],end[MAX];
    int root,L;
    int newnode()
    {
        for(int i=0; i<256; i++)
            Next[L][i]=-1;
        end[L++]=0;
        return L-1;
    }
    void init()
    {
        L=0;
        root=newnode();
    }
    void insert(int buf[],int len)
    {
        int now=root;
        for(int i=0; i<len; i++)
        {
            if(Next[now][buf[i]]==-1)
                Next[now][buf[i]]=newnode();
            now=Next[now][buf[i]];
        }
        end[now]++;
    }

    void build()
    {
        queue<int> Q;
        fail[root]=root;
        for(int i=0; i<256; i++)
        {
            if(Next[root][i]==-1)
                Next[root][i]=root;
            else
            {
                fail[Next[root][i]]=root;
                Q.push(Next[root][i]);
            }
        }
        while(!Q.empty())
        {
            int now=Q.front();
            Q.pop();
            for(int i=0; i<256; i++)
            {
                if(Next[now][i]==-1)
                    Next[now][i]=Next[fail[now]][i];
                else
                {
                    fail[Next[now][i]]=Next[fail[now]][i];
                    Q.push(Next[now][i]);
                }
            }
        }
    }

    int query(int code[],int len)
    {
        int vis[MAX];
        int ans = 0;
        memset(vis,0,sizeof(vis));
        for(int i = 0, cur = 0; i<len; i++)
        {
            int index = code[i];
            cur = Next[cur][index];
            for(int j = cur; j; j = fail[j])
            {
                if(!vis[j])
                {
                    ans += end[j];
                    vis[j] = 1;
                }
            }
        }
        return ans;
    }



};

Trie t;
int vr[MAX];//病毒数组
char buf[MAX];//字符串
int ath[MAX];//文件数组

int getVal(char ch)//获取编号
{
    if(ch>='A'&&ch<='Z')
        return ch-65;
    else if(ch>='a'&&ch<='z')
        return ch-71;
    else if(ch>='0'&&ch<='9')
        return ch+4;
    else if(ch=='+')
        return 62;
    else if(ch=='/')
        return 63;
    else
        return -1;
}

int dtc(char buf[],int dd[])//解码
{
    bool tmp[100000];
    int k,i,j,num=0,ecnt=0,p=0;
    int len=strlen(buf);
    memset(tmp,0,sizeof(tmp));
    for(i=0; i<len; i++)
    {
        k=(i+1)*6-1;
        if(buf[i]=='=')
            ecnt++;
        num=getVal(buf[i]);
        if(num!=-1)
            for(j=0; j<6; j++)
            {
                tmp[k--]=num%2;
                num/=2;
            }
    }
    k=(len-ecnt)*6;
    num=0;
    for(i=0; i<k; i++)
    {
        num+=tmp[i];
        if((i+1)%8==0)
        {
            dd[p++]=num;
            num=0;
        }
        num*=2;
    }
    return p;
}


int main()
{
    int n,m,num;

    while(~scanf("%d",&n))
    {
        t.init();
        for(int i=0; i<n; i++)
        {
            scanf("%s",buf);
            num=dtc(buf,vr);
            t.insert(vr,num);
        }
        t.build();
        scanf("%d",&m);
        for(int i=0; i<m; i++)
        {
            scanf("%s",buf);
            num=dtc(buf,ath);
            printf("%d\n",t.query(ath,num));
        }
        printf("\n");
    }
    return 0;
}




你可能感兴趣的:(ZOJ,the,virus,自动机,ac,Detect,3430)