ZOJ 3430 Detect the Virus ac 自动机

Detect the Virus(ac 自动机)
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Appoint description: 

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.

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 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;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值