Detect the Virus
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.
解题思路:
巨烦的一题。
读题读半天。
转换写半天。
题意就是给定一个字符串,但是是加密过的。然后按照规则解码出原来的字符串。
但是原来的字符串的ASCII码值可以为0!所以不能用字符串去表示。
应该用一个整数数组去表示。
剩下的就是在这个数组上建一个AC自动机,标记出现了多少种病毒就ok了。
AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) print f("%d\n", n)
#define pc(n) print f("%c", n)
#define pdd(n,m) print f("%d %d", n, m)
#define pld(n) print f("%int d\n", n)
#define pldd(n,m) print f("%int d %int d\n", n, m)
#define sld(n) scanf("%int d",&n)
#define sldd(n,m) scanf("%int d%int d",&n,&m)
#define slddd(n,m,k) scanf("%int d%int d%int d",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define aint (x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
#define pii map<int ,int >
#define mk make_pair
#define rtl rt<<1
#define rtr rt<<1|1
#define Max(x,y) (x)>(y)?(x):(y)
//#define int long long
typedef pair<int ,int > PII;
typedef long long ll;
typedef unsigned long long ull ;
typedef long double ld;
const int MOD = 1e9 + 7;
const int mod = 1e8 + 7;
const double eps = 1e-9;
const int INF = 0x3f3f3f3f3f3f3f3f;
//const int inf = 0x3f3f3f3f;
inline int read(){int ret = 0, sgn = 1;char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')sgn = -1;ch = getchar();}
while (ch >= '0' && ch <= '9'){ret = ret*10 + ch - '0';ch = getchar();}
return ret*sgn;}
inline void Out(int a){if(a>9) Out(a/10);putchar(a%10+'0');}
int qmul(int a,int b,int mod){int res=0;while(b){if(b&1)res=(res+a)%mod;a=(a+a)%mod;b>>=1;}return res;}
int qpow(int m,int k,int mod){int res=1%mod,t=m%mod;while(k){if(k&1)res=qmul(res,t,mod);t=qmul(t,t,mod);k>>=1;}return res;}
int gcd(int a,int b){if(b > a) swap(a,b); return b==0?a : gcd(b,a%b);}
int lcm(int a,int b){return a/gcd(a,b)*b;}
int inv(int x,int mod){return qpow(x,mod-2,mod)%mod;}
//const int N = 3e3+15;
int t = 1,cas = 1;
int n,m;
const int N = 300;
int tt[2560050];
int tmp[2560050];
const int MAXN = 200050;
char s[2560050];
map<int,int> mark;
char ID[67] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"};
struct Trie{
int next[MAXN][N],fail[MAXN*N],end[MAXN*N];
int root;
int tot;
int newnode()
{
for(int i=0;i<N;i++)
next[tot][i]=-1;
end[tot++]=0;
return tot-1;
}
void init()
{
tot=0;
root=newnode();
}
void insert(int buf[],int id)
{
int now=root;
for(int i=0;buf[i] != -1;i++)
{
int k=buf[i];
if(next[now][k]==-1)
next[now][k]=newnode();
now=next[now][k];
}
end[now]++;
}
void build()
{
queue<int> que;
fail[root]=root;
for(int i=0;i<N;i++)
if(next[root][i]==-1)
next[root][i]=root;
else
{
fail[next[root][i]]=root;
que.push(next[root][i]);
}
while(!que.empty())
{
int now = que.front();
que.pop();
for(int i=0;i<N;i++)
if(next[now][i]==-1)
next[now][i]=next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
que.push(next[now][i]);
}
}
}
int query(int buf[])
{
int now=root;
int res=0;
for(int i=0;buf[i] != -1;i++)
{
now=next[now][buf[i]];
int temp=now;
while(temp!=root)
{
//res+=end[temp];
if(!mark[temp] && end[temp])
{
res ++;
mark[temp] = 1;
}
//end[temp]=0;//模式串只在主串中匹配一次就可以了
temp=fail[temp];
}
}
return res;
}
};
Trie ac;
int Id(char x){
for(int i = 0 ; i< 64 ; i ++){
if(ID[i] == x){
return i;
}
}
}
void stoi(char *s){ // 解码字符串
int n = strlen(s);
int cnt = 0;
for(int i = 0 ; i < n ;i ++){
if(s[i] == '='){
cnt += 8;
tmp[i] = 0;
}
else
tmp[i] = Id(s[i]);
}
cnt = (n*6-cnt)/8;
int last = 0;
int pos = 0;
for(int i = 0 ; pos < cnt ;pos++, i++){
if(last == 0){
tt[pos] = (tmp[i]<<2)+(tmp[i+1]>>4);
last = 2;
}
else if(last == 2){
tt[pos] = ((tmp[i]%16)<<4)+(tmp[i+1]>>2);
last = 4;
}
else if(last == 4){
tt[pos] = ((tmp[i]%4)<<6)+(tmp[i+1]);
i ++;
last = 0;
}
}
tt[cnt] = -1;
// for(int i = 0 ; i < cnt ; i ++){
// cout<<tt[i]<<" ";
// }
// cout<<endl;
}
signed main(){
//scanf("%d",&t);
while(t--){
while(~scanf("%d",&n)){
ac.init();
for(int i = 0 ; i < n ;i ++){
scanf("%s",s);
stoi(s);
ac.insert(tt,i+1);
}
ac.build();
scanf("%d",&m);
for(int i = 0 ; i < m ; i ++){
mark.clear();
scanf("%s",s);
stoi(s);
cout<<ac.query(tt)<<endl;
}
cout<<endl;
}
}
return 0;
}