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, whereKIs 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 3KBytes.
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 00011000
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 fileInfileIn base64 and output resultOUTFILE.
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 integerN(0 <=N<= 512). In the nextNDistinct 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 integerM(1 <=M<= 128). In the followingMLines, 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, outputMLines.ITh line contains the number of kinds of virus detected inITh file.
Output a blank line after each case.
Sample Input
3YmFzZTY0dmlydXM=dDog1dGVzdDogdmlydXMu1QA==2QA==ICAgICAgICA=
Sample output
210
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.
Question: give n encoded template strings, and then ask m times. Each time you enter an encoded text string, ask before encoding, the number of template strings that appear in the text string.
Analysis: I think the difficulty of this question is not to build an AC automatic machine, but to decode the encoded string into the original text. During encoding, each character in the string is converted to a decimal integer, and each integer is converted to an eight-digit, two-digit integer. Then these two-digit numbers are connected, take six digits from the beginning each time. If there are less than six digits, add 0 to the back. Then convert the six digits into a new decimal integer, the new integer is the ASCII code of the encoded characters. If the number of characters before encoding is not a multiple of 3, add '=' After encoding ', the number of characters is divided by the remainder of 3. Because the ASCII value of the decoded character ranges from 0 ~ 255, so it is better not to use strings to store decoded content. Use int to save better.
#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;const int kind = 256; //the maximum number of letterconst int N = 50010;struct Node{ int next[kind]; int fail; int cnt; void build_node() { for(int i = 0; i < kind; i++) next[i] = 0; fail = 0; cnt = 0; }} node[N];int digit[N], code[N], Size, vis[N];char file[N], vir[N];int fun(char ch) //convert char to int{ 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; return 63;}void change(char *str){ int i, j, len, t; vector<int> v; memset(digit, 0, sizeof(digit)); for(len = strlen(str); str[len-1] == '='; len--) ; str[len] = '\0'; for(i = 0; i < len; i++) v.push_back(fun(str[i])); for(i = 0; i < v.size(); i++) { for(j = 6 * (i + 1) - 1; j >= 6 * i; j--) { if(v[i]&1) digit[j] = 1; v[i] >>= 1; } } int k = v.size() * 6 / 8; //the number of letter before encode for(i = 0; i < k; i++) { for(t = 0, j = 8 * i; j < 8 * (i + 1); j++) t = (t << 1) + digit[j]; code[i] = t; } code[i] = -1;}void Insert(){ int cur, index, i; for(cur = i = 0; code[i] >= 0; i++) { index = code[i]; if(node[cur].next[index] == 0) { node[++Size].build_node(); node[cur].next[index] = Size; } cur = node[cur].next[index]; } node[cur].cnt++;}void build_ac_automation(int root){ queue<int> q; q.push(root); while(!q.empty()) { int cur = q.front(); q.pop(); for(int i = 0; i < kind; i++) { if(node[cur].next[i]) { int p = node[cur].next[i]; if(cur) node[p].fail = node[node[cur].fail].next[i]; q.push(p); } else node[cur].next[i] = node[node[cur].fail].next[i]; } }}int Query(){ int ans = 0; for(int i = 0, cur = 0; code[i] >= 0; i++) { int index = code[i]; cur = node[cur].next[index]; for(int j = cur; j; j = node[j].fail) { if(!vis[j]) { ans += node[j].cnt; vis[j] = 1; } } } return ans;}int main(){ int n, m; while(~scanf("%d",&n)) { node[0].build_node(); Size = 0; for(int i = 1; i <= n; i++) { scanf("%s",vir); change(vir); Insert(); } build_ac_automation(0); scanf("%d",&m); while(m--) { memset(vis, 0, sizeof(vis)); scanf("%s", file); change(file); printf("%d\n", Query()); } printf("\n"); } return 0;}