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.

Value012345678910111213141516171819202122232425262728293031
EncodingABCDEFGHIJKLMNOPQRSTUVWXYZabcdef
Value3233343536373839404142434445464748495051525354555657585960616263
Encodingghijklmnopqrstuvwxyz0123456789+/

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组经过编码后的病毒串:

编码就是将原串转成二进制后取每6位进行转化成10进制后与上面表格对应的字符串,末尾不足6位补0,且原串个数为3k+1则在编码后的串里增加'==',若个数为3k+2则增加'=',给定的模式串也是编码后的串,所以需要进行反编码然后就是赤裸裸的AC自动机模版了。

注意:转换后的字符,是0~255的(需要用unsignedchar类型)..包括一些转义字符..譬如'\0'的,所以不能用strlen函数求其长度,开始没有注意到,一直Segmentation Fault,真是弄伤心了看见别人代码用的黑色背景,挺好看的,试试。。。

 

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <queue>

using namespace std;

#define maxn 256

char str[3000];

unsigned char buf[3000];

struct Trie

{

    int next[550*64][maxn];

    int fail[550*64];    //失配指针

    int end[550*64];     //记录数组

    

    int root; //根结点指针

    int L;    //总长度

    int NewNode()  //获取新结点并初始化

    {

        for(int i=0;i<maxn;i++)

        {

            next[L][i]=-1;

        }

        end[L]=-1;

        return L++;

    }

    void Init() //初始化

    {

        L=0;

        root=NewNode();

    }

    int  change(unsignedchar *des, char *sour,int len)

    {

        int t=0;

        for(int i=0;i<len;i++)

        {

            if(sour[i]>='A' && sour[i]<='Z')

                sour[i]-='A';

            

            else if(sour[i]>='a' && sour[i]<='z')

                sour[i]=sour[i]-'a'+26;

            

            else if(sour[i]>='0' && sour[i]<='9')

                sour[i]=sour[i]-'0'+52;

            

            else if(sour[i]=='+')

                sour[i]=62;

            

            else

                sour[i]=63;

        }

        

        for(int i=0;i<len;i+=4)

        {

            

           //3*8==4*6(位) 所以以4个字符为一个周期进行处理

            

            des[t++]=((sour[i]<<2)|(sour[i+1]>>4));//每个字符当做6位来处理(实际是8位),取8位(则第一个字符需要左移2位,需要下个字符的高4位(前两位没用))

            if(i+2<len)

                des[t++]=((sour[i+1]<<4)| (sour[i+2]>>2));

            if(i+3<len)

                des[t++]=((sour[i+2]<<6)| sour[i+3]);

        }

        return t;

        

    }

    void Insert(unsignedchar *s,int id,int len)

    {

       // int len=strlen(s);

        int j=root;

        for(int i=0;i<len;i++)

        {

            if(next[j][s[i]]==-1) //不存在该结点

            {

                next[j][s[i]]=NewNode();

            }

            j=next[j][s[i]];

        }

        end[j]=id;  //记录其id

    }

    void Build()

    {

        queue<int>q;

        fail[root]=root;  //根结点失配指针指向自己

       //根结点的孩子入队,其失配指针指向自己

        for(int i=0;i<maxn;i++)

        {

            if(next[root][i]==-1) //不存在该孩子

            {

                next[root][i]=root;//指向自己

            }

            else

            {

                fail[next[root][i]]=root;//失配指针指向自己

                q.push(next[root][i]); //孩子入队

            }

        }

        int j;

        while(!q.empty())

        {

            j=q.front();

            q.pop();

            for(int i=0;i<maxn;i++)

            {

                if(next[j][i]==-1) //不存在该孩子,指向其父结点失配指针所指向的结点(该结点也有孩子i)

                {

                    next[j][i]=next[fail[j]][i];

                }

                else

                {

                    fail[next[j][i]]=next[fail[j]][i];

                    q.push(next[j][i]);

                }

            }

        }

    }

    

    bool used[550];

    void query(unsignedchar *buf,int n,int len)

    {

       // int len=strlen(buf);

        int j=root;

        int temp;

        memset(used,0, sizeof(used));

        for(int i=0;i<len;i++)

        {

            j=next[j][buf[i]];

            temp=j;

            while(temp!=root)

            {

                if(end[temp]!=-1)//该单词或字符在Trie中出现了

                {

                    used[end[temp]]=1;

                }

                temp=fail[temp]; //继续找后缀串

            }

        }

        int  num=0;

        for(int i=0;i<n;i++)

        {

            if(used[i])

            {

                num++;

            }

        }

        printf("%d\n",num);

    }

};



Trie ac;

int main()

{

    int n,m,len;

    while (scanf("%d",&n)!=EOF)

    {

        ac.Init();

        for (int i=0; i<n; i++)

        {

            scanf("%s",str);

            len=strlen(str);

            while (str[len-1]=='=')

            {

                len--;

            }

            str[len]='\0';

            len=ac.change(buf,str,len);

            ac.Insert(buf, i,len);

        }

        ac.Build();

        scanf("%d",&m);

        for (int i=0; i<m; i++)

        {

            scanf("%s",str);

            len=strlen(str);

            while (str[len-1]=='=')

            {

                len--;

            }

            str[len]='\0';

            len=ac.change(buf,str,len);

            ac.query(buf, n,len);

        }

        printf("\n");

        

    }

    return 0;

}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值