ZOJ 3430 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










십일
십이
십삼
십사
십오
십육
십칠
십팔
십구
이십
21
22
23
24
25
26
27
28
29
삼십
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
사십
41
42
43
44
45
46
47
48
49
오십
51
52
53
54
55
56
57
58
59
육십
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









+
/
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.
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
#define prt(k) cout<<#k"="<<k<<endl;
#define ll long long
const int Node=4e4+3;
const int N= Node;
int next[Node][256],fail[N],end[N];
int L,root;
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 u=root;
    for(int i=0;i<len;i++)
    {
        if(next[u][buf[i]]==-1)
            next[u][buf[i]]=newnode();
        u=next[u][buf[i]];
    }
    end[u]++;
}
void build()
{
    queue<int> q;
    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 u=q.front(); q.pop();
        for(int i=0;i<256;i++)
        {
            if(next[u][i]==-1)
                next[u][i]=next[fail[u]][i];
            else {
                fail[next[u][i]]=next[fail[u]][i];
                q.push(next[u][i]);
            }
        }
    }
}
int tmp[Node];
int query(int buf[],int len)
{
    for(int i=0;i<L;i++) tmp[i]=end[i];
    int res=0,u=root;
    for(int i=0;i<len;i++)
    {
        u=next[u][buf[i]];
        for(int fu=u;fu!=root;fu=fail[fu])
        {
            res+=tmp[fu];
            tmp[fu]=0;
        }
    }
    return res;
}
int idx(char a)
{
    if('A'<=a&&a<='Z') return a-'A';
    if('a'<=a&&a<='z') return a-'a'+26;
    if('0'<=a&&a<='9') return a-'0'+52;
    if(a=='+') return 62;
    if(a=='/') return 63;
}
int buf[N],buflen;
int n;
char s[4000];
int bit[32000];
void decoder()
{
    int len = strlen(s);
    int l = 0;
    for (int i = 0;i < len;i++)
    {
        if (s[i] == '=')    break;
        int val = idx(s[i]);
        for (int j = 0;j < 6;j++)
            bit[l++] = ((val>>(5-j))&1);
    }
    l = (l/8)*8;
    len = 0;
    for (int i = 0;i < l;i += 8)
    {
        int val = 0;
        for (int j = 0;j < 8;j++)
        {
            val <<= 1;
            val |= bit[i+j];
        }
        buf[len++] = val;
    }
    buflen = len;
}
int m;
int main()
{
    while(scanf("%d",&n)==1)
    {
        init();
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            decoder();
            insert(buf,buflen);
        }
        build();
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%s",s);
            decoder();
            int ans=query(buf,buflen);
            printf("%d
",ans); //cout<<ans<<endl; } putchar(10); } }

좋은 웹페이지 즐겨찾기