码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ3430---Detect the Virus

时间:2015-02-10 15:21:51      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:ac自动机-字符串

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

处理字符串然后AC自动机处理,很坑爹的题

/*************************************************************************
    > File Name: zoj3430.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年02月10日 星期二 12时45分52秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

char str[10010];
int temp[10010];
char buf[10010];
bool vis[555];
int table[200];
int m, lt;
const int MAX_NODE = 512 * 65;
const int CHILD_NUM = 256;

struct AC_Automation
{
    int next[MAX_NODE][CHILD_NUM];
    int fail[MAX_NODE];
    int end[MAX_NODE];
    int root, L;

    int newnode()
    {
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            next[L][i] = -1;
        }
        end[L++] = 0;
        return L - 1;
    }

    void init ()
    {
        L = 0;
        root = newnode();
    }

    void Build_Trie (int buf[], int len, int x)
    {
        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] = x;
    }

    void Build_AC ()
    {
        queue <int> qu;
        fail[root] = root;
        while (!qu.empty())
        {
            qu.pop();
        }
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            if (next[root][i] == -1)
            {
                next[root][i] = root;
            }
            else
            {
                fail[next[root][i]] = root;
                qu.push (next[root][i]);
            }
        }
        while (!qu.empty())
        {
            int now = qu.front();
            qu.pop();
            for (int i = 0; i < CHILD_NUM; ++i)
            {
                if (next[now][i] == -1)
                {
                    next[now][i] = next[fail[now]][i];
                }
                else
                {
                    fail[next[now][i]] = next[fail[now]][i];
                    qu.push(next[now][i]);
                }
            }
        }
    }

    void Match (int buf[], int len)
    {
        int now = root;
        for (int i = 0; i < len; ++i)
        {
            now = next[now][buf[i]];
            int tmp = now;
            while (tmp != root)
            {
                if (end[tmp])
                {
                    vis[end[tmp]] = 1;
                }
                tmp = fail[tmp];
            }
        }
    }
}AC;


void change(char buf[])
{

    int len = strlen(buf);
    int t = 0;
    int ln = 0;
    lt = 0;
    int tt;
    for(int i = 0 ;i < len; ++i)
    {
        if(buf[i] != ‘=‘)
        {
            if(buf[i] >= ‘A‘ && buf[i] <= ‘Z‘)
                tt =  buf[i] -‘A‘;
            else if(buf[i] >= ‘a‘ && buf[i] <= ‘z‘)
                tt = buf[i]-‘a‘ + 26;
            else if(buf[i] >= ‘0‘ && buf[i] <= ‘z‘)
                tt= buf[i] -‘0‘ + 52;
            else if(buf[i] == ‘+‘)
                tt = 62;
            else if(buf[i] == ‘/‘)tt = 63;
            if(6 < 8 - t)
            {
                ln += (tt << (8-t-6));
                t += 6;
            }else{
                ln += (tt >> (6-8+t));
                temp[lt++] = ln;
                ln = (tt&((1<<(6-8+t)) -1)) << (8-(6-8+t));
                t = (6-8+t);
            }
        }
    }
}

int main ()
{
    int n, q;
    while (~scanf("%d", &n))
    {
        AC.init();
        for (int i = 1; i <= n; ++i)
        {
            scanf("%s", str);
            change(str);
            AC.Build_Trie(temp, lt, i);
        }
        AC.Build_AC();
        scanf("%d", &q);
        while (q--)
        {
            memset (vis, 0, sizeof(vis));
            scanf("%s", str);
            change(str);
            int ans = 0;
            AC.Match(temp, lt);
            for (int i = 1; i <= n; ++i)
            {
                ans += vis[i];
            }
            printf("%d\n", ans);
        }
        printf("\n");
    }
    return 0;
}

ZOJ3430---Detect the Virus

标签:ac自动机-字符串

原文地址:http://blog.csdn.net/guard_mine/article/details/43703065

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!