ZOJ 3430 Detect the Virus
传送门: 3430 Detect the Virus 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.
Author: WU, Jun
Contest: ZOJ Monthly, November 2010
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Solution: A-C Automation
注意:
1.弄清题意,“If these binary bytes can be found in the email attachment (binary data), then the attachment contains the virus.“题意是,将base64编码的模式串与文本串翻译成ASCII编码的字符串,再进行匹配。并不是将两者翻译成二进制(0-1串)进行匹配。
2.也可将模式串与文本串翻译成0-1串做匹配,但注意匹配的位置必须是整字节处。这样做代码更简洁,内存与时间都更优(Maybe, 因为字符集是{0, 1})。
#include<bits/stdc++.h>
using namespace std;
const char *m1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int m2[];
void build_map(){
for(int i=; m1[i]; i++){
m2[m1[i]]=i;
}
}
const int MAX_N=1e6+, ls=, lt=2e4, MAX_M=;
bool s[ls], t[lt], used[MAX_M];
char v[ls], f[lt];
int q[MAX_N], head, tail;
struct node
{
int pre, id, pos[], last;
void init(){
id=;
memset(pos, , sizeof(pos));
}
};
node trie[MAX_N];
void b2b(char ch, bool *b, int &i){
if(ch=='=') i-=;
else{
int p=;
while(p){
b[i++]=m2[ch]&p;
p>>=;
}
}
}
int trans(char *s, bool *b){
int i=, j=;
for(; s[i]; i++){
b2b(s[i], b, j);
}
//for(int i=0; i<j; i++) printf("%d", b[i]);
//puts("");
return j;
}
int build_trie(int N){
int tot=, now, len;
trie[tot].init();
for(int id=; id<=N; id++){
scanf("%s", v);
len=trans(v, s);
now=;
for(int i=; i<len; i++){
int &nt=trie[now].pos[s[i]]; //error-prone
now=nt?nt:(trie[++tot].init(), nt=tot);
}
trie[now].id=id;
}
return tot;
}
void build_ac(){
head=tail=;
trie[].last=;
for(int i=; i<; i++){
int &nt=trie[].pos[i];
if(nt){
trie[nt].pre=;
trie[nt].last=;
q[tail++]=nt;
}
}
int pre;
while(head!=tail){
int &top=q[head++];
for(int i=; i<; i++){
int &nt=trie[top].pos[i];
pre=trie[top].pre;
if(!nt) nt=trie[pre].pos[i];
else{
q[tail++]=nt;
while(!trie[pre].pos[i]&&pre) pre=trie[pre].pre;
pre=trie[nt].pre=trie[pre].pos[i];
int &last=trie[nt].last;
last=trie[pre].id?pre:trie[pre].last; //error-prone
}
}
}
}
void get_ans(int pre, int &cnt){ //error-prone
pre=trie[pre].id?pre:trie[pre].last;
while(pre && !used[trie[pre].id]){
used[trie[pre].id]=true;
cnt++;
pre=trie[pre].last;
}
}
void match(int N){
while(N--){
scanf("%s", f);
int len=trans(f, t);
memset(used, , sizeof(used));
int cnt=;
for(int i=, pre=; i<len; i++){
pre=trie[pre].pos[t[i]];
if(i%==){
get_ans(pre, cnt);
}
}
printf("%d\n", cnt);
}
} int main(){
//freopen("in", "r", stdin);
build_map();
int N, M;
while(~scanf("%d", &N)){
build_trie(N);
build_ac();
scanf("%d", &M);
match(M);
puts("");
}
return ;
}
ZOJ 3430 Detect the Virus的更多相关文章
- zoj 3430 Detect the Virus(AC自己主动机)
题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制.然后以8为一个数值,又一次形成字符 串,推断给定询问串是否含有字符 ...
- ZOJ - 3430 Detect the Virus —— AC自动机、解码
题目链接:https://vjudge.net/problem/ZOJ-3430 Detect the Virus Time Limit: 2 Seconds Memory Limit: 6 ...
- 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自动机)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3430 题意:给你n个编码后的模式串,和m个编码后的主串,求原来主 ...
- ZOJ 3430 Detect the Virus 【AC自动机+解码】
解码的那些事儿,不多说. 注意解码后的结果各种情况都有,用整数数组存储,char数组会超char类型的范围(这个事最蛋疼的啊)建立自动机的时候不能用0来判断结束. #include <cstdi ...
- ZOJ 3430 Detect the Virus(AC自动机 + 模拟)题解
题意:问你主串有几种模式串.但是所有串都是加密的,先解码.解码过程为:先把串按照他给的映射表变成6位数二进制数,然后首尾衔接变成二进制长串,再8位8位取变成新的数,不够的补0.因为最多可能到255,所 ...
- 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】3430 Detect the Virus
动态建树MLE.模仿别人的代码模板各种原因wa后,终于AC. #include <iostream> #include <cstdio> #include <cstrin ...
- Detect the Virus ZOJ - 3430 AC自动机
One day, Nobita found that his computer is extremely slow. After several hours' work, he finally fou ...
随机推荐
- Hadoop和Spark的异同
谈到大数据,相信大家对Hadoop和Apache Spark这两个名字并不陌生.但我们往往对它们的理解只是提留在字面上,并没有对它们进行深入的思考,下面不妨跟我一块看下它们究竟有什么异同. 解决问题的 ...
- 各浏览器对typeof运算符的实现差异
1,IE6/7/8中typeof运算符对BOM对象如window,document,location,history等对象的方法返回“object”,标准浏览器都返回“function”. 1 2 3 ...
- GeoServer+MySQL安装及配置过程
GeoServer的安装配置请参考 http://simen-net.iteye.com/blog/609078 由于大部分WEBGIS不仅仅只是一个地图的显示,还需要一些业务处理,会有用到数据库地方 ...
- 学习Shell脚本编程(第1期)_Shell命令行书写规则
Shell命令行的书写规则 对Shell命令行基本功能的理解有助于编写更好的Shell程序,在执行Shell命令时多个命令可以在一个命令行上运行,但此时要使用分号(:)分隔命令,例如: [root@l ...
- 技能获取与C语言学习情况
你有什么技能比大多人(超过90%以上)更好? 仔细回想了一下自己到目前为止的学习生涯,好像真的没有什么技能能够比90%以上的人好. 初中高中学过很多东西,但是能够算得上专精的却着实没有.小学参加过计算 ...
- 实现checkbox组件化(Component)
之前我写了一篇自定义checkbox的文章,通过css3实现自定义的checkbox,并没有使用当今流行的Reactjs, 或者Vuejs之类的进行组件化.但是很显然,这样封装的checkbox组件复 ...
- [AHOI2013]立方体(三维bit)
[Ahoi2013]立方体 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 130 Solved: 55[Submit][Status] Descrip ...
- 【Groovy基础系列】 Groovy运算符
?运算符 在java中,有时候为了避免出现空指针异常,我们通常需要这样的技巧: if(rs!=null){ rs.next() … … } 在groovy中,可以使用?操作符达到同样的目的: rs?. ...
- javascript与服务器3
一, 带参数的XMLHTTP请求 1, 进行get请求 get请求最常见的是在浏览器地址栏中输入URL并打开页面时,这就是向服务器发送一个get请求. 它的限制是URL最大长度不能超过2048字符(2 ...
- u1-nav-js
'use strict';define([ 'jquery'], function($) { var nav = { init : function() { $("#burger-menu& ...