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


    Author: WU, Jun
    Contest: ZOJ Monthly, November 2010

    AC自动机的题目。

    就是编码转换有点麻烦。

    而且这个要用一个字节256个点来表示状态

    //============================================================================
    // Name        : ZOJ.cpp
    // Author      : 
    // Version     :
    // Copyright   : Your copyright notice
    // Description : Hello World in C++, Ansi-style
    //============================================================================
    
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    using namespace std;
    
    struct Trie
    {
        int next[520*64][256],fail[520*64],end[520*64];
        int root,L;
        int newnode()
        {
            for(int i = 0;i < 256;i++)
                next[L][i] = -1;
            end[L++] = -1;
            return L-1;
        }
        void init()
        {
            L = 0;
            root = newnode();
        }
        void insert(unsigned char buf[],int len,int id)
        {
            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] = id;
        }
        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]);
                    }
            }
        }
        bool used[520];
        int query(unsigned char buf[],int len,int n)
        {
            memset(used,false,sizeof(used));
            int now = root;
            for(int i = 0;i < len;i++)
            {
                now = next[now][buf[i]];
                int temp = now;
                while( temp!=root )
                {
                    if(end[temp] != -1)
                        used[end[temp]]=true;
                    temp = fail[temp];
                }
            }
            int res = 0;
            for(int i = 0;i < n;i++)
                if(used[i])
                    res++;
            return res;
        }
    };
    
    unsigned char buf[2050];
    int tot;
    char str[4000];
    unsigned char s[4000];
    unsigned char Get(char ch)
    {
        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;
        else return 63;
    }
    void change(unsigned char str[],int len)
    {
        int t=0;
        for(int i=0;i<len;i+=4)
        {
            buf[t++]=((str[i]<<2)|(str[i+1]>>4));
            if(i+2 < len)
                buf[t++]=( (str[i+1]<<4)|(str[i+2]>>2) );
            if(i+3 < len)
                buf[t++]= ( (str[i+2]<<6)|str[i+3] );
        }
        tot=t;
    }
    Trie ac;
    int main()
    {
    //    freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
        int n,m;
        while(scanf("%d",&n) == 1)
        {
            ac.init();
            for(int i = 0;i < n;i++)
            {
                scanf("%s",str);
                int len = strlen(str);
                while(str[len-1]=='=')len--;
                for(int j = 0;j < len;j++)
                {
                    s[j] = Get(str[j]);
                }
                change(s,len);
                ac.insert(buf,tot,i);
            }
            ac.build();
            scanf("%d",&m);
            while(m--)
            {
                scanf("%s",str);
                int len=strlen(str);
                while(str[len-1]=='=')len--;
                for(int j = 0;j < len;j++)
                    s[j] = Get(str[j]);
                change(s,len);
                printf("%d
    ",ac.query(buf,tot,n));
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    crontab使用
    python 学习 第一课
    php调用阿里大鱼 接口curl
    thinkphp 动态 级联
    nginx重启
    linux查看 文件夹大小
    mysql convert
    mysql 数据库导入 导出,解决 导入 错误问题
    .net 更新数据 ado.net parameter
    PHP的超全局变量$_SERVER
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3158844.html
Copyright © 2020-2023  润新知