zoj3430之AC自动机

来源:互联网 发布:java程序开发培训 编辑:程序博客网 时间:2024/04/28 21:11
ZOJ Problem Set - 3430
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.

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.

题目是给先你n组经过编码后的病毒串,
编码就是将原串转成二进制后取每6位进行转化成10进制后与上面表格对应的字符串,末尾不足6位补0,
且原串个数为3k+1则在编码后的串里增加'==',若个数为3k+2则增加'=',给定的模式串也是编码后的串
所以需要进行反编码然后就是赤裸裸的AC自动机模版了

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<iomanip>#define INF 99999999using namespace std;const int MAX=5000+10;char s[MAX];//输入的字符串 int temp[MAX],size;//编码后的字符串,储存ASCII码int Map[256];//映射A,B,C... =〉0,1,2...struct TrieNode{int num,id;//记录病毒串个数和最后访问该串的序号TrieNode *fail,*next[256];//失败指针和下一个节点 }*root,Node[MAX*10];void ReEncode(char *a){//反编码 temp[0]=0;for(int i=0,len=0,x=0;a[i] && a[i] != '=';++i){len+=6,x=(x<<6)|Map[a[i]];if(len>=8){temp[++temp[0]]=(x>>(len-8))&0xff;//0xff是恰好8位1,x>>(len-8)保证是每8位进行&运算len-=8; }}}TrieNode *New_TrieNode(){//分配节点 memset(&Node[size],0,sizeof(TrieNode));return &Node[size++];}void InsertNode(int *a){//插入病毒串 TrieNode *p=root;for(int i=1;i<=a[0];++i){if(!p->next[a[i]])p->next[a[i]]=New_TrieNode();p=p->next[a[i]];}++p->num;}void Build_AC(){//建立AC自动机 TrieNode *p,*next;queue<TrieNode *>q;q.push(root);while(!q.empty()){p=q.front();q.pop();for(int i=0;i<256;++i){if(p->next[i]){next=p->fail;while(next && !next->next[i])next=next->fail;p->next[i]->fail=next?next->next[i]:root;q.push(p->next[i]);}}}}int SearchTrie(int *a,int id){//查询模式串包含病毒串个数int sum=0;TrieNode *p=root,*next;for(int i=1;i<=a[0];++i){while(p != root && !p->next[a[i]])p=p->fail;p=p->next[a[i]];if(!p)p=root;next=p;while(next != root && next->id != id){sum+=next->num;next->id=id;next=next->fail;}}return sum;}void Init(){for(int i=0;i<26;++i)Map[i+'A']=i;for(int i=26;i<52;++i)Map[i-26+'a']=i;for(int i=52;i<62;++i)Map[i-52+'0']=i;Map['+']=62,Map['/']=63;}int main(){Init();int n;while(scanf("%d",&n)!=EOF){size=0;root=New_TrieNode();for(int i=0;i<n;++i){scanf("%s",s);//输入的是编码后的字符串 ReEncode(s);//进行反编码InsertNode(temp);}Build_AC();//建立AC自动机 cin>>n;for(int i=1;i<=n;++i){scanf("%s",s);ReEncode(s);printf("%d\n",SearchTrie(temp,i));}printf("\n");}return 0;}


原创粉丝点击