您的位置:首页 > 其它

zoj 3430 Detect the Virus AC自动机

2015-07-20 10:52 288 查看
点击打开链接题目链接

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.
题意:
给出转码后的字符串 问转码前的文本串含有多少个基本的字符串

mle了好几发 后来用了kuangbin的模板就过了- -

转码的过程有点麻烦= =

#include<cstdio>
#include<cstring>
#include<cstring>
#include<algorithm>
#include<queue>
#define MAXN 55555
using namespace std;
int num[MAXN],code[MAXN];
bool digit[MAXN];
int change(char ch){
    if(ch<='Z'&&ch>='A')
        return ch-'A';
    else if(ch<='z'&&ch>='a')
        return 26+ch-'a';
    else if(ch<='9'&&ch>='0')
        return 52+ch-'0';
    else if(ch=='+')
        return 62;
    return 63;
}
void translate(char *str){
    int l=strlen(str);
    while(str[l-1]=='=')
        l--;
    for(int i=0;i<l;i++)
        num[i]=change(str[i]);
    memset(code,0,sizeof(code));
    memset(digit,0,sizeof(digit));
    int cnt=0;
    for(int i=0;i<l;i++){
        for(int j=1;j<=6;j++){
            if(num[i]&1)
                digit[(i+1)*6-j]=1;
            else digit[(i+1)*6-j]=0;
            num[i]>>=1;
        }
    }
     for(int i=0;i<l*6/8;i++){
        for(int j=0;j<8;j++){
            code[i]<<=1;
            code[i]+=digit[i*8+j];
        }
    }
    cnt=l*6/8;
    code[cnt]=-1;
}
struct Trie{
    int next[555*64][256],fail[555*64],ed[555*64];
    int root,now;
    int newnode(){
        for(int i=0;i<256;i++)
            next[now][i]=-1;
        fail[now]=-1;
        ed[now++]=-1;
        return now-1;
    }
    void init(){
        now=0;
        root=newnode();
    }
    void tree_insert(int id){
        int i=0;
        int p=root;
        while(code[i]!=-1){
            int index=code[i];
            if(next[p][index]==-1)
                next[p][index]=newnode();
            p=next[p][index];
            i++;
        }
        ed[p]=id;
    }
    void build_ac_automachine(){
        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 temp=q.front();
            q.pop();
            for(int i=0;i<256;i++){
                if(next[temp][i]==-1)
                    next[temp][i]=next[fail[temp]][i];
                else{
                    fail[next[temp][i]]=next[fail[temp]][i];
                    q.push(next[temp][i]);
                }
            }
        }
    }
    bool vis[555];
    void solve(char *str,int n){
        translate(str);
        memset(vis,0,sizeof(vis));
        int i=0,ans=0;
        int p=root;
        while(code[i]!=-1){
            p=next[p][code[i]];
            int temp=p;
            while(temp!=root){
                if(ed[temp]!=-1)
                    vis[ed[temp]]=1;
                temp=fail[temp];
            }
            i++;
        }
        for(int i=0;i<n;i++)
            if(vis[i]==1)
                ans++;
        printf("%d\n",ans);
    }
}ac;
int main(){
    int n;
    int m;
    char str[MAXN];
    while(scanf("%d",&n)!=EOF){
        ac.init();
        for(int i=0;i<n;i++){
            scanf("%s",str);
            translate(str);
            int j=0;
            ac.tree_insert(i);
        }
        ac.build_ac_automachine();
        int m;
        scanf("%d",&m);
        for(int i=0;i<m;i++){
            scanf("%s",str);
            ac.solve(str,n);
        }
        puts("");
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: