ZOJ - 3430 Detect the Virus —— AC自动机、解码
题目链接: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自动机、解码的更多相关文章
- zoj 3430 Detect the Virus(AC自己主动机)
题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制.然后以8为一个数值,又一次形成字符 串,推断给定询问串是否含有字符 ...
- ZOJ 3430 Detect the Virus
传送门: Detect the Virus ...
- ZOJ 3430 Detect the Virus(AC自动机)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3430 题意:给你n个编码后的模式串,和m个编码后的主串,求原来主 ...
- zoj 3430 Detect the Virus(AC自己主动机)
Detect the Virus Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita found that his co ...
- ZOJ 3430 Detect the Virus 【AC自动机+解码】
解码的那些事儿,不多说. 注意解码后的结果各种情况都有,用整数数组存储,char数组会超char类型的范围(这个事最蛋疼的啊)建立自动机的时候不能用0来判断结束. #include <cstdi ...
- ZOJ 3430 Detect the Virus(AC自动机 + 模拟)题解
题意:问你主串有几种模式串.但是所有串都是加密的,先解码.解码过程为:先把串按照他给的映射表变成6位数二进制数,然后首尾衔接变成二进制长串,再8位8位取变成新的数,不够的补0.因为最多可能到255,所 ...
- ZOJ 3228 Searching the String(AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 KB Little jay really hates to d ...
- ZOJ 4114 Detect the Virus(AC自动机)
Detect the Virus Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita found that his co ...
- ZOJ 3494 BCD Code(AC自动机 + 数位DP)题解
题意:每位十进制数都能转化为4位二进制数,比如9是1001,127是 000100100111,现在问你,在L到R(R <= $10^{200}$)范围内,有多少数字的二进制表达式不包含模式串. ...
随机推荐
- 第1章 为什么创造WPF、第2章 XAML揭秘
1.2 步入WPF 下面是WPF的一些亮点: 广泛整合:各种媒体类型都能组合起来并一起呈现 与分辨率无关:因为WPF使用矢量图形 硬件加速:WPF是基于Direct3D创建的,工作全部是由GPU完成的 ...
- node.js之http-server
我们有时候会遇到这种情况,一个html文件在本地打开时,测试平常的功能还行,但是,一涉及到ajax请求,就算你是请求本地的json文件,他都会涉及到跨域的问题,浏览器本身就限制了本地打开时,不允许跨域 ...
- 移植MonkeyRunner的图片对照和获取子图功能的实现-UiAutomator/Robotium篇
依据前一篇文章<移植MonkeyRunner的图片对照和获取子图功能的实现-Appium篇>所述,由于Appium和MonkeyRunner有一个共同点--代码控制流程都是在client实 ...
- 15、Spring Boot使用Druid和监控配置【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52001740目录(?)[-] 1添加Maven依赖 或jar包 2配置数据源相关信息 3 ...
- 原生JavaScript技巧大收集100个
原生JavaScript技巧大收集 1.原生JavaScript实现字符串长度截取function cutstr(str, len) { var temp; var icount = 0; var p ...
- 使用matlab进行mex编译时的路径问题mexopts
matlab和vs 进行混合编程时总须要使用matlab编译mexFunction.cpp文件. 这些文件免不了使用include下的*.h和lib下的*.lib文件.举例说明.这次我 ...
- 1M网速等于多少K
http://zhidao.baidu.com/question/157400316.html&__bd_tkn__=65ac453b343794385019e962bfb06bb8c710d ...
- spring源码解析之IOC容器(一)
学习优秀框架的源码,是提升个人技术水平必不可少的一个环节.如果只是停留在知道怎么用,但是不懂其中的来龙去脉,在技术的道路上注定走不长远.最近,学习了一段时间的spring源码,现在整理出来,以便日后温 ...
- Android 开发小工具之:Tools 属性 (转)
Android 开发小工具之:Tools 属性 http://blog.chengyunfeng.com/?p=755#ixzz4apLZhfmi 今天来介绍一些 Android 开发过程中比较有用但 ...
- Spring IOC源代码具体解释之容器依赖注入
Spring IOC源代码具体解释之容器依赖注入 上一篇博客中介绍了IOC容器的初始化.通过源代码分析大致了解了IOC容器初始化的一些知识.先简单回想下上篇的内容 加载bean定义文件的过程.这个过程 ...