【ZOJ】3430 Detect the Virus AC自动机

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.

Value012345678910111213141516171819202122232425262728293031
EncodingABCDEFGHIJKLMNOPQRSTUVWXYZabcdef
Value3233343536373839404142434445464748495051525354555657585960616263
Encodingghijklmnopqrstuvwxyz0123456789+/

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 60in 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


传送门:【ZOJ】3430 Detect the Virus


题目大意:

给你n个通过base64编码的模式串,再给m个同样编码过的目标串,问在每一个目标串中能找到多少的模式串


题目分析:

除了编码就是一道模板AC自动机了。

好开心~第一道AC的AC自动机。接下来开始AC自动机的训练。


代码如下:


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

const int maxN = 50005 ;
const int maxW = 256 ;
const int maxQ = 1000000 ;

struct Trie {
	int next[ maxN ][ maxW ] ;
	int fail[ maxN ] ;
	int end[ maxN ] ;
	int P , root ;
	int head , tail ;
	int Q[ maxN ] ;
	int hash[ maxW << 1 | 1 ] ;
	
	int New () {
		for ( int i = 0 ; i < maxW ; ++ i )
			next[ P ][ i ] = -1 ;
		end[ P ] = -1 ;
		return P ++ ;
	}
	
	void Init () {
		P = 0 ;
		root = New () ;
	}
	
	void Insert ( unsigned char s[] , int len , int id ) {
		int now = root ;
		for ( int i = 0 ; i < len ; ++ i ) {
			int idx = s[ i ] ;
			if ( next[ now ][ idx ] == -1 ) next[ now ][ idx ] = New () ;
			now = next[ now ][ idx ] ;
		}
		end[ now ] = id ;
	}
	
	void Build () {
		head = tail = 0 ;
		fail[ root ] = root ;
		for ( int i = 0 ; i < maxW ; ++ i ) {
			if ( next[ root ][ i ] == -1 ) {
				next[ root ][ i ] = root ;
			}
			else {
				fail[ next[ root ][ i ] ] = root ;
				Q[ tail ++ ] = next[ root ][ i ] ;
			}
		}
		while ( head != tail ) {
			int now = Q[ head ++ ] ;
			for ( int i = 0 ; i < maxW ; ++ i ) {
				if ( next[ now ][ i ] == -1 ) {
					next[ now ][ i ] = next[ fail[ now ] ][ i ] ;
				}
				else {
					fail[ next[ now ][ i ] ] = next[ fail[ now ] ][ i ] ;
					Q[ tail ++ ] = next[ now ][ i ] ;
				}
			}
		}
	}
	
	int Query ( unsigned char s[]  , int len , int n ) {
		int now = root , res = 0 ;
		memset ( hash , 0 , sizeof hash ) ;
		for ( int i = 0 ; i < len ; ++ i ) {
			now = next[ now ][ s[i] ] ;
			int tmp = now ;
			while ( tmp != root ) {
				if ( ~end[ tmp ] ) hash[ end[ tmp ] ] = 1 ;
				tmp = fail[ tmp ] ;
			}
		}
		for ( int i = 0 ; i < n ; ++ i ) if ( hash[ i ] ) ++ res ;
		return res ;
	}
} ;

Trie AC ;
unsigned char str[ maxN ] ;
char s[ maxN ] ;
unsigned char ss[ maxN ] ;

unsigned char Get ( char x ) {
	if ( x >= 'A' && x <= 'Z' ) return x - 'A'  + 0 ;
	if ( x >= 'a' && x <= 'z' ) return x - 'a' + 26 ;
	if ( x >= '0' && x <= '9' ) return x - '0' + 52 ;
	if ( x == '+' ) return 62 ;
	return 63 ;
}

int Encode ( unsigned char s[] , int len ) {
	int num = 0 ;
	for ( int i = 0 ; i < len ; i += 4 ) {
		str[ num ++ ] = ( s[ i ] << 2 ) | ( s[ i + 1 ] >> 4 ) ;
		if ( i + 2 < len ) str[ num ++ ] = ( s[ i + 1 ] << 4 ) | ( s[ i + 2 ] >> 2 ) ;
		if ( i + 3 < len ) str[ num ++ ] = ( s[ i + 2 ] << 6 ) | s[ i + 3 ] ;
	}
	return num ;
}

void work () {
	int n , m ;
	while ( ~scanf ( "%d" , &n ) ) {
		AC.Init () ;
		for ( int i = 0 ; i < n ; ++ i ) {
			scanf ( "%s" , s ) ;
			int len = strlen ( s ) ;
			while ( s[ len - 1 ] == '=' ) -- len ;
			for ( int j = 0 ; j < len ; ++ j ) ss[ j ] = Get ( s[ j ] ) ;
			int num = Encode ( ss , len ) ;
			AC.Insert ( str , num , i ) ;
		}
		AC.Build () ;
		scanf ( "%d" , &m ) ;
		for ( int i = 0 ; i < m ; ++ i ) {
			scanf ( "%s" , s ) ;
			int len = strlen ( s ) ;
			while ( s[ len - 1 ] == '=' ) -- len ;
			for ( int j = 0 ; j < len ; ++ j ) ss[ j ] = Get ( s[ j ] ) ;
			int num = Encode ( ss , len ) ;
			printf ( "%d\n" , AC.Query ( str , num , n ) ) ;
		}
		printf ( "\n" ) ;
	}
}

int main () {
	work () ;
	return 0 ;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值