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 followingM 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.

-------------------------------------------------------------

注意,字符为'\0'不一定是字符串结尾。

对访问过的节点进行标记,访问后还原。

-------------------------------------------------------------

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

using namespace std;

unsigned char word[3333333];
unsigned char s[3333333];
unsigned char tcode[3333333];

const int kind = 256;


struct node
{
    node *fail;
    node *next[kind];
    int count;
    bool visit;
    node()
    {
        fail = NULL;
        count = 0;
        visit = false;
        memset(next,NULL,sizeof(next));
    }
};
node* query_temp_que[1111];
void insert(unsigned char *str,node *root,int slen)
{
    node *p=root;
    int i,index;
    for (i=0;i<slen;i++)
    {
        index = (int)str[i];
        if(p->next[index]==NULL) p->next[index]=new node();
        p=p->next[index];
    }
    p->count++;
}

//寻找失败指针
void build_ac_automation(node *root)
{
    int i;
    queue<node *>Q;
    root->fail = NULL;
    Q.push(root);
    while(!Q.empty())
    {
        node *temp=Q.front();//q[head++];//取队首元素
        Q.pop();
        node *p=NULL;
        for(i=0; i<kind; i++)
        {
            if(temp->next[i]!=NULL)//寻找当前子树的失败指针
            {
                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.push(temp->next[i]);
            }
        }
    }
}

//询问str中包含n个关键字中多少种即匹配
int query(unsigned char *str,node *root,int slen)
{
    int i,cnt = 0,index;
    int head,tail;
    head=tail=0;
    node *p = root;
    for (i=0;i<slen;i++)
    {
        index = (int)str[i];
        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->visit)//寻找到当前位置为止是否出现病毒关键字
        {
            cnt+=temp->count;
            temp->visit=true;
            query_temp_que[tail++]=temp;
            temp=temp->fail;
        }
    }
    while (head<tail)
    {
        node* cur=query_temp_que[head++];
        cur->visit=false;
    }
    return cnt;
}

void encode(unsigned char word[],unsigned char s[],int& slen)
{
    int p=0;
    int dcl=0;
    for (int i=0;word[i];i++)
    {
        if (word[i]>='A'&&word[i]<='Z')
        {
            tcode[i]=word[i]-'A';
        }
        if (word[i]>='a'&&word[i]<='z')
        {
            tcode[i]=word[i]-'a'+26;
        }
        if (word[i]>='0'&&word[i]<='9')
        {
            tcode[i]=word[i]-'0'+52;
        }
        if (word[i]=='+')
        {
            tcode[i]=62;
        }
        if (word[i]=='/')
        {
            tcode[i]=63;
        }
        if (word[i]=='=')
        {
            tcode[i]=0;
            dcl++;
        }
    }
    for (int i=0;word[i];i+=4)
    {
        s[p++]= (tcode[i] <<2) | (tcode[i+1] >>4);
        s[p++]= ((tcode[i+1] & 0xf) <<4) | (tcode[i+2] >>2);
        s[p++]= ((tcode[i+2] & 0x3) <<6) | (tcode[i+3]);
    }
    s[p]='\0';
    while (dcl--)
    {
        p--;
        s[p]='\0';
    }
    slen=p;
}

node* root;

int main()
{
    int n,m;
    while (~scanf("%d",&n))
    {
        int slen=0;
        root=new node;
        for (int i=1;i<=n;i++)
        {
            scanf("%s",word);
            encode(word,s,slen);
            insert(s,root,slen);
        }
        build_ac_automation(root);
        scanf("%d",&m);
        for (int i=1;i<=m;i++)
        {
            scanf("%s",word);
            encode(word,s,slen);
            int ans=query(s,root,slen);
            printf("%d\n",ans);
        }
        printf("\n");
    }
    return 0;
}






posted on 2013-04-21 14:38  电子幼体  阅读(150)  评论(0编辑  收藏  举报

导航