ZOJ 3430 Detect the Virus

10 篇文章 0 订阅
6 篇文章 0 订阅

题目链接:ZOj3430

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 Mlines, 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.



题意:给出一个病毒串和病毒串各字母的对应的数值,=号都不处理。还原成二进制后每8位重新组合成一个字符,问下面一堆母串中各有多少模式串呢。

题目分析:因为转换成的字母也包含回车空格之类的特殊字符,所以稳妥的方法是将之都转换为ascii码开256叉的字典树跑ac自动机。

本题主要在于病毒串与模式串的转换,我写的很丑不过也懒得改了。

//
//  main.cpp
//  ZOJ3430
//
//  Created by teddywang on 16/4/16.
//  Copyright © 2016年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
typedef struct node{
    int num;
    node *next[256];
    node *fail;
}trienode;
node Node[50040];
map<char,int>t;
trienode *root;
int head,tail,num,n,m,size;
int code[5100];

trienode *insert_node()
{
    Node[size].num =0;
    
    Node[size].fail=NULL;
    for(int i=0;i<256;i++)
        Node[size].next[i]=NULL;
    return &Node[size++];
}

void init()
{
    num=size=0;
    root=insert_node();
}

void insert(int *s,int len)
{
    trienode *r=root,*t;
    for(int i=0;i<len;i++)
    {
        if(r->next[s[i]]==NULL)
        {
            t=insert_node();
            r->next[s[i]]=t;
            r=t;
        }
        else r=r->next[s[i]];
    }
    r->num=1;
}

int recode(char *s)
{
    int i=0,j=0,len=0;
    int p[16];
    while(s[i]&&s[i]!='=')
    {
        int temp=t[s[i]];
        for(int k=32;k>=1;k/=2)
        {
            if(temp>=k)
            {
                temp-=k;
                p[j++]=1;
            }
            else p[j++]=0;
            if(j==8)
            {
                for(int l=0;l<8;l++)
                {
                    if(p[l]==1)
                    code[len]+=1<<(7-l);
                }
                j=0;len++;
            }
        }
        i++;
    }
    return len;
}

void build_ac()
{
    trienode *p;
    queue<trienode *> q;
    q.push(root);
    while(!q.empty())
    {
        p=q.front();
        q.pop();
        for(int i=0;i<256;i++)
        {
            if(p->next[i]!=NULL)
            {
                if(p==root)
                    p->next[i]->fail=root;
                else
                {
                    trienode *temp=p->fail;
                    while(temp!=NULL)
                    {
                        if(temp->next[i]!=NULL)
                        {
                            p->next[i]->fail=temp->next[i];
                            break;
                        }
                        temp=temp->fail;
                    }
                }
                q.push(p->next[i]);
            }
            else
            {
                if(p==root)
                    p->next[i]=root;
                else
                    p->next[i]=p->fail->next[i];
            }
        }
    }
}

int query(int *s,int len)
{
    trienode *p=root;
    int ans=0;
    for(int i=0;i<len;i++)
    {
        while(p->next[s[i]]==NULL&&p!=root)
            p=p->fail;
        p=p->next[s[i]];
        trienode *temp=p;
        while(temp!=NULL)
        {
            if(temp->num)
            {
                ans+=temp->num;
                temp->num=0;
            }
            else break;
            temp=temp->fail;
        }
    }
    return ans;
}

int main()
{
    for(int i=0;i<26;i++)
    {
        t['A'+i]=i;
        t['a'+i]=i+26;
    }
    for(int i=0;i<10;i++)
        t['0'+i]=i+52;
    t['+']=62;t['/']=63;
    while(cin>>n)
    {
        init();
        for(int i=0;i<n;i++)
        {
            memset(code,0,sizeof(code));
            char s1[2100];
            scanf("%s",s1);
            int len=recode(s1);
            insert(code,len);
        }
        build_ac();
        cin>>m;
        for(int i=0;i<m;i++)
        {
            memset(code,0,sizeof(code));
            char s2[2100];
            scanf("%s",s2);
            int len=recode(s2);
            num=query(code,len);
            cout<<num<<endl;
        }
        cout<<endl;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值