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 interestInput
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.
<h4< dd="">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.
<h4< dd="">Sample Input
3 YmFzZTY0 dmlydXM= dDog 1 dGVzdDogdmlydXMu 1 QA== 2 QA== ICAgICAgICA=
<h4< dd="">Sample Output
2 1 0
<h4< dd="">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.
这题的难点就在字符串的转化
这个函数怎么工作的呢 以示例aGVsbG8为例说明(其实匡斌这个版本是网上看的写的最简单的==)
我用%d输出了str[i] 发现是各个字母正常的ASCII码共7个数 26 6 21 44 27 6 60对应011010 00|0110 0101|01 101100 | 011011 00|0110 1111|00
然后需要转换成8位的:011010|00 0110|0101 01|101100 | 011011|00 0110|1111
26实际上是00011010其他的依次类推!
为什么我们要i+=4. 因为3*8 = 4*6 ,所以4为一个周期进行转化就可以了
其实就是模拟题中说的过程 只不过是以4个原字母为单位 (因为需要6-位>8位 那么四个字母会最终转化为三个字母 )即:
h是由a这六位与G的前两位组成(a需要向前串两位,G取其ASCII码右移4位的结果,“|”在本题中即相连),e是由G的后四位和V的前四位组成, L(其实是小写的为了便于区分)是由V的后两位和8的六位组成~~我还傻呼呼去查位或“|”是怎么回事=_=//
by the way char在Linux系统上默认为unsigned 在windows下默认为signed
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 8 struct Trie 9 { 10 int next[520*64][256],fail[520*64],end[520*64]; 11 int root,L; 12 int newnode() 13 { 14 for(int i = 0;i < 256;i++) 15 next[L][i] = -1; 16 end[L++] = -1; 17 return L-1; 18 } 19 void init() 20 { 21 L = 0; 22 root = newnode(); 23 } 24 void insert(unsigned char buf[],int len,int id) 25 { 26 int now = root; 27 for(int i = 0;i < len;i++) 28 { 29 if(next[now][buf[i]] == -1) 30 next[now][buf[i]] = newnode(); 31 now = next[now][buf[i]]; 32 } 33 end[now] = id; 34 } 35 void build() 36 { 37 queue<int>Q; 38 fail[root] = root; 39 for(int i = 0;i < 256;i++) 40 if(next[root][i] == -1) 41 next[root][i] = root; 42 else 43 { 44 fail[next[root][i]]=root; 45 Q.push(next[root][i]); 46 } 47 while(!Q.empty()) 48 { 49 int now = Q.front(); 50 Q.pop(); 51 for(int i = 0;i < 256;i++) 52 if(next[now][i] == -1) 53 next[now][i] = next[fail[now]][i]; 54 else 55 { 56 fail[next[now][i]] = next[fail[now]][i]; 57 Q.push(next[now][i]); 58 } 59 } 60 } 61 bool used[520]; 62 int query(unsigned char buf[],int len,int n) 63 { 64 memset(used,false,sizeof(used)); 65 int now = root; 66 for(int i = 0;i < len;i++) 67 { 68 now = next[now][buf[i]]; 69 int temp = now; 70 while( temp!=root ) 71 { 72 if(end[temp] != -1) 73 used[end[temp]]=true; 74 temp = fail[temp]; 75 } 76 } 77 int res = 0; 78 for(int i = 0;i < n;i++) 79 if(used[i]) 80 res++; 81 return res; 82 } 83 }; 84 85 unsigned char buf[2050]; 86 int tot; 87 char str[4000]; 88 unsigned char s[4000]; 89 unsigned char Get(char ch) 90 { 91 if( ch>='A'&&ch<='Z' )return ch-'A'; 92 if( ch>='a'&&ch<='z' )return ch-'a'+26; 93 if( ch>='0'&&ch<='9' )return ch-'0'+52; 94 if( ch=='+' )return 62; 95 else return 63; 96 } 97 void change(unsigned char str[],int len) // 这一步可以自己模拟一下。要注意的是虽然我们说的是六位,但是实际上八位 98 { 99 int t=0; 100 for(int i=0;i<len;i+=4) 101 { 102 buf[t++]=((str[i]<<2)|(str[i+1]>>4)); 103 if(i+2 < len) 104 buf[t++]=( (str[i+1]<<4)|(str[i+2]>>2) ); 105 if(i+3 < len) 106 buf[t++]= ( (str[i+2]<<6)|str[i+3] ); 107 } 108 tot=t; 109 } 110 Trie ac; 111 int main() 112 { 113 int n,m; 114 while(scanf("%d",&n) == 1) 115 { 116 ac.init(); 117 for(int i = 0;i < n;i++) 118 { 119 scanf("%s",str); 120 int len = strlen(str); 121 while(str[len-1]=='=')len--; 122 for(int j = 0;j < len;j++) 123 { 124 s[j] = Get(str[j]); 125 } 126 change(s,len); 127 ac.insert(buf,tot,i); 128 } 129 ac.build(); 130 scanf("%d",&m); 131 while(m--) 132 { 133 scanf("%s",str); 134 int len=strlen(str); 135 while(str[len-1]=='=')len--; 136 for(int j = 0;j < len;j++) 137 s[j] = Get(str[j]); 138 change(s,len); 139 printf("%d ",ac.query(buf,tot,n)); 140 } 141 printf(" "); 142 } 143 return 0; 144 }