ZOJ - 3430 Detect the Virus —— AC自动机、解码
题目链接:https://vjudge.net/problem/ZOJ-3430
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 Mlines, 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
题意:
1.将八个位base的一串单词编码成一串六个位base的单词,并且六个位base中每个值对应一个ASCII符(题目给出),当初始串以八个位展开后长度不能整除6时,最后一个六个位base码在后面补全0。如果八个位base原串长度不满足等于3*k,那么编码过后的六进制base串就需要在后面补全‘=’,当3*k+1时,补1个;3*k+2时补2个。
2.给出编码过后的n个单词,有m个查询,每次查询一个编码后的串含有多少种单词?
题解:
1.先将单词解码,然后再插入AC自动机中。
2.对于每次询问,将长串解码,然后再查询。注意:由于有多个查询,所以每次查询都不能修改AC自动机中原有的信息!
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const double EPS = 1e-6; 15 const int INF = 2e9; 16 const LL LNF = 2e18; 17 const int MOD = 1e9+7; 18 const int MAXN = 512*64+10; 19 20 struct Trie 21 { 22 int sz, base; 23 int next[MAXN][256], fail[MAXN], end[MAXN], tmpend[MAXN]; 24 int root, L; 25 int newnode() 26 { 27 for(int i = 0; i<sz; i++) 28 next[L][i] = -1; 29 end[L++] = false; 30 return L-1; 31 } 32 void init(int _sz, int _base) 33 { 34 sz = _sz; 35 base = _base; 36 L = 0; 37 root = newnode(); 38 } 39 void insert(int buf[]) 40 { 41 int len = buf[0]; 42 int now = root; 43 for(int i = 1; i<=len; i++) 44 { 45 if(next[now][buf[i]] == -1) next[now][buf[i]] = newnode(); 46 now = next[now][buf[i]]; 47 } 48 end[now] = true; 49 } 50 void build() 51 { 52 queue<int>Q; 53 fail[root] = root; 54 for(int i = 0; i<sz; i++) 55 { 56 if(next[root][i] == -1) next[root][i] = root; 57 else fail[next[root][i]] = root, Q.push(next[root][i]); 58 } 59 while(!Q.empty()) 60 { 61 int now = Q.front(); 62 Q.pop(); 63 for(int i = 0; i<sz; i++) 64 { 65 if(next[now][i] == -1) next[now][i] = next[fail[now]][i]; 66 else fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]); 67 } 68 } 69 } 70 71 int query(int buf[]) 72 { 73 memcpy(tmpend, end, sizeof(tmpend)); //因为有多次查询,所以要保存原来的信息。 74 int len = buf[0]; 75 int now = root; 76 int res = 0; 77 for(int i = 1; i<=len; i++) 78 { 79 now = next[now][buf[i]]; 80 int tmp = now; 81 while(tmp != root) 82 { 83 res += end[tmp]; 84 end[tmp] = false; 85 tmp = fail[tmp]; 86 } 87 } 88 memcpy(end, tmpend, sizeof(end)); 89 return res; 90 } 91 }; 92 93 int M[128]; //六进制base 对应 ASCII符 94 void Init() 95 { 96 for(int i = 0; i<26; i++) M['A'+i] = i; 97 for(int i = 0; i<26; i++) M['a'+i] = 26+i; 98 for(int i = 0; i<10; i++) M['0'+i] = 52+i; 99 M['+'] = 62; M['/'] = 63; 100 } 101 102 int s[3000]; //不能用char存,因为最大值为255,char存不了。 103 int* decode(char buf[]) //解码,将字符以六个位base展开,每八个位作为一个新值。 104 { 105 int lenbuf = strlen(buf); 106 int x = 0, bits = 0; 107 s[0] = 0; 108 for(int i = 0; i<lenbuf&&buf[i]!='='; i++) //当读到“=”时,有用信息已经读取完。 109 { 110 x <<= 6; 111 x |= M[buf[i]]; 112 bits += 6; 113 if(bits>=8) 114 { 115 s[++s[0]] = (x>>(bits-8)); 116 x &= (1<<(bits-8))-1; 117 bits -= 8; 118 } 119 } 120 return s; 121 } 122 123 Trie ac; 124 char buf[3000]; 125 int main() 126 { 127 Init(); 128 int n, m; 129 while(scanf("%d", &n)!=EOF) 130 { 131 ac.init(256,0); 132 for(int i = 1; i<=n; i++) 133 { 134 scanf("%s", buf); 135 ac.insert(decode(buf)); 136 } 137 ac.build(); 138 scanf("%d", &m); 139 for(int i = 1; i<=m; i++) 140 { 141 scanf("%s", buf); 142 printf("%d\n", ac.query(decode(buf))); 143 } 144 putchar('\n'); 145 } 146 }