ZOJ 3430 Detect the Virus
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.
Author: WU, Jun
Contest: ZOJ Monthly, November 2010
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Solution: A-C Automation
注意:
1.弄清题意,“If these binary bytes can be found in the email attachment (binary data), then the attachment contains the virus.“题意是,将base64编码的模式串与文本串翻译成ASCII编码的字符串,再进行匹配。并不是将两者翻译成二进制(0-1串)进行匹配。
2.也可将模式串与文本串翻译成0-1串做匹配,但注意匹配的位置必须是整字节处。这样做代码更简洁,内存与时间都更优(Maybe, 因为字符集是{0, 1})。
1 #include<bits/stdc++.h> 2 using namespace std; 3 const char *m1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 4 int m2[256]; 5 void build_map(){ 6 for(int i=0; m1[i]; i++){ 7 m2[m1[i]]=i; 8 } 9 } 10 const int MAX_N=1e6+10, ls=500, lt=2e4, MAX_M=520; 11 bool s[ls], t[lt], used[MAX_M]; 12 char v[ls], f[lt]; 13 int q[MAX_N], head, tail; 14 struct node 15 { 16 int pre, id, pos[2], last; 17 void init(){ 18 id=0; 19 memset(pos, 0, sizeof(pos)); 20 } 21 }; 22 node trie[MAX_N]; 23 void b2b(char ch, bool *b, int &i){ 24 if(ch=='=') i-=2; 25 else{ 26 int p=32; 27 while(p){ 28 b[i++]=m2[ch]&p; 29 p>>=1; 30 } 31 } 32 } 33 int trans(char *s, bool *b){ 34 int i=0, j=0; 35 for(; s[i]; i++){ 36 b2b(s[i], b, j); 37 } 38 //for(int i=0; i<j; i++) printf("%d", b[i]); 39 //puts(""); 40 return j; 41 } 42 int build_trie(int N){ 43 int tot=0, now, len; 44 trie[tot].init(); 45 for(int id=1; id<=N; id++){ 46 scanf("%s", v); 47 len=trans(v, s); 48 now=0; 49 for(int i=0; i<len; i++){ 50 int &nt=trie[now].pos[s[i]]; //error-prone 51 now=nt?nt:(trie[++tot].init(), nt=tot); 52 } 53 trie[now].id=id; 54 } 55 return tot; 56 } 57 void build_ac(){ 58 head=tail=0; 59 trie[0].last=0; 60 for(int i=0; i<2; i++){ 61 int &nt=trie[0].pos[i]; 62 if(nt){ 63 trie[nt].pre=0; 64 trie[nt].last=0; 65 q[tail++]=nt; 66 } 67 } 68 int pre; 69 while(head!=tail){ 70 int &top=q[head++]; 71 for(int i=0; i<2; i++){ 72 int &nt=trie[top].pos[i]; 73 pre=trie[top].pre; 74 if(!nt) nt=trie[pre].pos[i]; 75 else{ 76 q[tail++]=nt; 77 while(!trie[pre].pos[i]&&pre) pre=trie[pre].pre; 78 pre=trie[nt].pre=trie[pre].pos[i]; 79 int &last=trie[nt].last; 80 last=trie[pre].id?pre:trie[pre].last; //error-prone 81 } 82 } 83 } 84 } 85 void get_ans(int pre, int &cnt){ //error-prone 86 pre=trie[pre].id?pre:trie[pre].last; 87 while(pre && !used[trie[pre].id]){ 88 used[trie[pre].id]=true; 89 cnt++; 90 pre=trie[pre].last; 91 } 92 } 93 void match(int N){ 94 while(N--){ 95 scanf("%s", f); 96 int len=trans(f, t); 97 memset(used, 0, sizeof(used)); 98 int cnt=0; 99 for(int i=0, pre=0; i<len; i++){ 100 pre=trie[pre].pos[t[i]]; 101 if(i%8==7){ 102 get_ans(pre, cnt); 103 } 104 } 105 printf("%d\n", cnt); 106 } 107 } 108 109 int main(){ 110 //freopen("in", "r", stdin); 111 build_map(); 112 int N, M; 113 while(~scanf("%d", &N)){ 114 build_trie(N); 115 build_ac(); 116 scanf("%d", &M); 117 match(M); 118 puts(""); 119 } 120 return 0; 121 }