题目链接:https://vjudge.net/problem/ZOJ-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 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自动机中原有的信息!

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MOD = 1e9+;
const int MAXN = *+; struct Trie
{
int sz, base;
int next[MAXN][], fail[MAXN], end[MAXN], tmpend[MAXN];
int root, L;
int newnode()
{
for(int i = ; i<sz; i++)
next[L][i] = -;
end[L++] = false;
return L-;
}
void init(int _sz, int _base)
{
sz = _sz;
base = _base;
L = ;
root = newnode();
}
void insert(int buf[])
{
int len = buf[];
int now = root;
for(int i = ; i<=len; i++)
{
if(next[now][buf[i]] == -) next[now][buf[i]] = newnode();
now = next[now][buf[i]];
}
end[now] = true;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ; i<sz; i++)
{
if(next[root][i] == -) next[root][i] = root;
else fail[next[root][i]] = root, Q.push(next[root][i]);
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
for(int i = ; i<sz; i++)
{
if(next[now][i] == -) next[now][i] = next[fail[now]][i];
else fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]);
}
}
} int query(int buf[])
{
memcpy(tmpend, end, sizeof(tmpend)); //因为有多次查询,所以要保存原来的信息。
int len = buf[];
int now = root;
int res = ;
for(int i = ; i<=len; i++)
{
now = next[now][buf[i]];
int tmp = now;
while(tmp != root)
{
res += end[tmp];
end[tmp] = false;
tmp = fail[tmp];
}
}
memcpy(end, tmpend, sizeof(end));
return res;
}
}; int M[]; //六进制base 对应 ASCII符
void Init()
{
for(int i = ; i<; i++) M['A'+i] = i;
for(int i = ; i<; i++) M['a'+i] = +i;
for(int i = ; i<; i++) M[''+i] = +i;
M['+'] = ; M['/'] = ;
} int s[]; //不能用char存,因为最大值为255,char存不了。
int* decode(char buf[]) //解码,将字符以六个位base展开,每八个位作为一个新值。
{
int lenbuf = strlen(buf);
int x = , bits = ;
s[] = ;
for(int i = ; i<lenbuf&&buf[i]!='='; i++) //当读到“=”时,有用信息已经读取完。
{
x <<= ;
x |= M[buf[i]];
bits += ;
if(bits>=)
{
s[++s[]] = (x>>(bits-));
x &= (<<(bits-))-;
bits -= ;
}
}
return s;
} Trie ac;
char buf[];
int main()
{
Init();
int n, m;
while(scanf("%d", &n)!=EOF)
{
ac.init(,);
for(int i = ; i<=n; i++)
{
scanf("%s", buf);
ac.insert(decode(buf));
}
ac.build();
scanf("%d", &m);
for(int i = ; i<=m; i++)
{
scanf("%s", buf);
printf("%d\n", ac.query(decode(buf)));
}
putchar('\n');
}
}

ZOJ - 3430 Detect the Virus —— AC自动机、解码的更多相关文章

  1. zoj 3430 Detect the Virus(AC自己主动机)

    题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制.然后以8为一个数值,又一次形成字符 串,推断给定询问串是否含有字符 ...

  2. ZOJ 3430 Detect the Virus

    传送门: Detect the Virus                                                                                ...

  3. ZOJ 3430 Detect the Virus(AC自动机)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3430 题意:给你n个编码后的模式串,和m个编码后的主串,求原来主 ...

  4. zoj 3430 Detect the Virus(AC自己主动机)

    Detect the Virus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his co ...

  5. ZOJ 3430 Detect the Virus 【AC自动机+解码】

    解码的那些事儿,不多说. 注意解码后的结果各种情况都有,用整数数组存储,char数组会超char类型的范围(这个事最蛋疼的啊)建立自动机的时候不能用0来判断结束. #include <cstdi ...

  6. ZOJ 3430 Detect the Virus(AC自动机 + 模拟)题解

    题意:问你主串有几种模式串.但是所有串都是加密的,先解码.解码过程为:先把串按照他给的映射表变成6位数二进制数,然后首尾衔接变成二进制长串,再8位8位取变成新的数,不够的补0.因为最多可能到255,所 ...

  7. ZOJ 3228 Searching the String(AC自动机)

    Searching the String Time Limit: 7 Seconds      Memory Limit: 129872 KB Little jay really hates to d ...

  8. ZOJ 4114 Detect the Virus(AC自动机)

    Detect the Virus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his co ...

  9. ZOJ 3494 BCD Code(AC自动机 + 数位DP)题解

    题意:每位十进制数都能转化为4位二进制数,比如9是1001,127是 000100100111,现在问你,在L到R(R <= $10^{200}$)范围内,有多少数字的二进制表达式不包含模式串. ...

随机推荐

  1. Python 第三方 http 库-Requests 学习

    Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...

  2. mysql rpm安装,以及修改charset

    http://my.oschina.net/u/1156660/blog/343154?fromerr=tmDGGiDL 修改charset: http://stackoverflow.com/que ...

  3. cocos2dx3.x使用cocostudio触摸事件不响应的奇葩问题

    刚刚使用3.1,发现了一些关于触摸的不同之处,对于习惯于2.x的人还是认为坑啊,简单总结一下: 使用cocostudio加进来的ui,当某个可触但不可见的时候,给他加入不论什么触摸监听事件都是依照不可 ...

  4. libGDX 模块概览

    本文章翻译自libGDX官方wiki,.转载请注明出处:http://blog.csdn.net/kent_todo/article/details/37940595 libGDX官方网址:http: ...

  5. How do you stop Ansible from creating .retry files in the home directory?

    There are two options that you can add to the [defaults] section of the ansible.cfg file that will c ...

  6. 数组方式使用jQuery对象

    一. 使用jQuery选择器获取结果是一个jQuery对象.然而,jQuery类库会让你感觉你正在使用一个定义了索引和长度的数组.在性能方面,建议使用简单的for或者while循环来处理,而不是$.e ...

  7. EMMC电路设计

    优秀文档: eMMC基础技术1:MMC简介 eMMC基础技术2:eMMC概述 一:供电电源时序 EMMC的供电有两种模式,且分两路工作,有VCC和VccQ.在规范上,上电时序是有要求的,如下图所示. ...

  8. rtems 4.11 IRQ (arm,beagle)

    arm IRQ入口在 cpukit/score/arm/arm_exec_interrupt.S 中,其中BSP最关心就是 bl bsp_interrupt_dispatch 这句,看看beagle ...

  9. C语言高速入门系列(四)

    C语言高速入门系列(四) C语言数组 ---------转载请注明出处:coder-pig 贴心小提示:假设图看不清晰可右键另存为,应该就非常清晰了; 注意上面的代码都要自己过一遍哦! 本节引言: 经 ...

  10. CentOS 配置网络

    1.编辑ifcfg-eth0 vi /etc/sysconfig/network-scripts/ifcfg-eth0 2.修改NOBOOT=yes 3.重启服务 service network re ...