题目链接:ZOj3430
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 interestHere 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 interestInput
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.
题意:给出一个病毒串和病毒串各字母的对应的数值,=号都不处理。还原成二进制后每8位重新组合成一个字符,问下面一堆母串中各有多少模式串呢。
题目分析:因为转换成的字母也包含回车空格之类的特殊字符,所以稳妥的方法是将之都转换为ascii码开256叉的字典树跑ac自动机。
本题主要在于病毒串与模式串的转换,我写的很丑不过也懒得改了。
//
// main.cpp
// ZOJ3430
//
// Created by teddywang on 16/4/16.
// Copyright © 2016年 teddywang. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
typedef struct node{
int num;
node *next[256];
node *fail;
}trienode;
node Node[50040];
map<char,int>t;
trienode *root;
int head,tail,num,n,m,size;
int code[5100];
trienode *insert_node()
{
Node[size].num =0;
Node[size].fail=NULL;
for(int i=0;i<256;i++)
Node[size].next[i]=NULL;
return &Node[size++];
}
void init()
{
num=size=0;
root=insert_node();
}
void insert(int *s,int len)
{
trienode *r=root,*t;
for(int i=0;i<len;i++)
{
if(r->next[s[i]]==NULL)
{
t=insert_node();
r->next[s[i]]=t;
r=t;
}
else r=r->next[s[i]];
}
r->num=1;
}
int recode(char *s)
{
int i=0,j=0,len=0;
int p[16];
while(s[i]&&s[i]!='=')
{
int temp=t[s[i]];
for(int k=32;k>=1;k/=2)
{
if(temp>=k)
{
temp-=k;
p[j++]=1;
}
else p[j++]=0;
if(j==8)
{
for(int l=0;l<8;l++)
{
if(p[l]==1)
code[len]+=1<<(7-l);
}
j=0;len++;
}
}
i++;
}
return len;
}
void build_ac()
{
trienode *p;
queue<trienode *> q;
q.push(root);
while(!q.empty())
{
p=q.front();
q.pop();
for(int i=0;i<256;i++)
{
if(p->next[i]!=NULL)
{
if(p==root)
p->next[i]->fail=root;
else
{
trienode *temp=p->fail;
while(temp!=NULL)
{
if(temp->next[i]!=NULL)
{
p->next[i]->fail=temp->next[i];
break;
}
temp=temp->fail;
}
}
q.push(p->next[i]);
}
else
{
if(p==root)
p->next[i]=root;
else
p->next[i]=p->fail->next[i];
}
}
}
}
int query(int *s,int len)
{
trienode *p=root;
int ans=0;
for(int i=0;i<len;i++)
{
while(p->next[s[i]]==NULL&&p!=root)
p=p->fail;
p=p->next[s[i]];
trienode *temp=p;
while(temp!=NULL)
{
if(temp->num)
{
ans+=temp->num;
temp->num=0;
}
else break;
temp=temp->fail;
}
}
return ans;
}
int main()
{
for(int i=0;i<26;i++)
{
t['A'+i]=i;
t['a'+i]=i+26;
}
for(int i=0;i<10;i++)
t['0'+i]=i+52;
t['+']=62;t['/']=63;
while(cin>>n)
{
init();
for(int i=0;i<n;i++)
{
memset(code,0,sizeof(code));
char s1[2100];
scanf("%s",s1);
int len=recode(s1);
insert(code,len);
}
build_ac();
cin>>m;
for(int i=0;i<m;i++)
{
memset(code,0,sizeof(code));
char s2[2100];
scanf("%s",s2);
int len=recode(s2);
num=query(code,len);
cout<<num<<endl;
}
cout<<endl;
}
}