您的位置:首页 > 其它

ZOJ-3430

2015-05-08 12:24 225 查看
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.

/**
题意:给你n个模版,然后再给出一个字符串,问该字符串包含几个模版
做法:AC自动机 字符串的转换有些麻烦,先将字符串转换成ASCII 然后根据相应的ASCII 转换成二进制,每一个是6位,不够加0,然后取8位为一个字符,求得的字符串为要的字符串
**/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#define maxn 520*64
using namespace std;
int n;
int tot = 0;
unsigned char ch1[maxn];
struct Tire
{
int next[maxn][256],end[maxn],fail[maxn];
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)
{
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 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   solve(char ch[],int len)
{
string str;
string str1;
str1 = "";
for(int i=0; i<len; i++)
{
str = "";
int res = int(ch[i]);
while(res)
{
str += res%2 + '0';
res /= 2;
}
while(str.length() != 6) str += "0";
reverse(str.begin(),str.end());
str1 += str;
}
int len1 = str1.length();
while(len1%8 !=0) len1--;
int mm = 0;
memset(ch1,'\0',sizeof(ch1));
for(int i=0; i<len1; i+=8)
{
int res = 0;
for(int j=0; j<8; j++)
{
res += (str1[i+j] -'0') * pow(2.0,7-j);
}
ch1[mm++] = char(res);
}
//cout<<ch1<<"\n";
tot = mm;
return ;
}

Tire ac;
char buf[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int m;
while(~scanf("%d",&n))
{
ac.init();
char xx[maxn];
for(int i=0; i<n; i++)
{
memset(xx,'\0',sizeof(xx));
scanf("%s",buf);
int len = strlen(buf);
while(buf[len-1]=='=')len--;
for(int j=0; j<len; j++)
{
xx[j] = Get(buf[j]);
}
solve(xx,len);
ac.insert(ch1,tot,i);
}
ac.build();
scanf("%d",&m);
while(m--)
{
scanf("%s",buf);
int len = strlen(buf);
memset(xx,'\0',sizeof(xx));
while(buf[len-1] == '=')
{
--len;
}
for(int j=0; j<len; j++)
{
xx[j] = Get(buf[j]);
}
solve(xx,len);
printf("%d\n",ac.query(ch1,tot));
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: