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}$)范围内,有多少数字的二进制表达式不包含模式串. ...
随机推荐
- mongodb文档的CRUD
本章会介绍对数据库移入或者移出数据的基本操作 向集合添加文档 从集合删除文档 更新现有的文档 为这些操作选择合适的安全级别 添加删除数据库 添加数据库 :use foo 如果存在foo 就use ...
- falsh,.swf文件修改z-index
<object style="z-index:-1;"> <param name="wmode" value="transparen ...
- UNP学习笔记(第一章 简介)
环境搭建 1.下载解压unpv13e.tar.gz 2.进入目录执行 ./configurecd lib //进入lib目录make //执行make命令 3.将生成的libunp.a静态库复制到/u ...
- js:"use strict"; 严格模式
http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html
- NightWatchJS(转)
关于Nightwatch? Nightwatch.js是一个测试web app和web 站点的自动化测试框架, 使用Node.js编写, 基于Selenium WebDriver API. 它是一个完 ...
- 字符串== equals
经常碰到比较字符串的题, eg: public class StringDemo{ private static final String MESSAGE = "taobao"; ...
- nginx for windows 配置多域名反向代理
调试了很久...哦耶 共享出来吧 其实 nginx反向代理同一ip多个域名,给header加上host就可以了 upstream test.test.cn { server 119. ...
- xcode常见报错调试【原创】
BMKGeoCodeSearch 反向地理编码一直失败 Location 申请的key的安全码与代码中build id不一样,修改成一样就OK了 新建一个类,将百度地图的定位功能封装起来以后,定位的代 ...
- JavaScript读书笔记(5)-Object Date
1.Object类型 (1)创建Object实例 第一种方式:new操作符后跟Object构造函数 var person=new Object(); person.name=”Nicholas”; p ...
- Centos 7.0防火墙问题
从Centos7开始,自带的防火墙从iptables更改成了firewall.一般在企业环境,出于人力和稳定性考虑,还是用成熟的技术比较稳妥. 以下是关闭firewall的方法 systemctl s ...