题目大意:
就是现在给出一些病毒样本的字符串(base64的形式),数量在512以内,然后是m个需要检测的样本,对于每组数据中的每个样本输出其包含几种不同的病毒,每组数据之后留一个空行
大致思路:
就是要先将base64状态下的各个病毒样本解码变成正常的字符串然后插入Trie树中,建立AC自动机对样本进行遍历即可。
这里由于base64转码之后可能出现char值为0的字符,这里我用的整数数组来代替的char数组
刚开始做的时候交了好多次都MLE了,悲剧..
代码如下:
Result : Accepted Memory : 33828 KB Time : 120 ms
/* * Author: Gatevin * Created Time: 2014/11/17 18:45:57 * File Name: Kagome.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; #define NODE_SIZE 512*65 #define CHARSET 256 #define VIRUS_NUMBER 520 int n; char s[3000]; int v[3000]; map <char, int> M; struct Trie { int next[NODE_SIZE][CHARSET], fail[NODE_SIZE], end[NODE_SIZE]; int L, root; bool vis[VIRUS_NUMBER]; int newnode() { for(int i = 0; i < CHARSET; i++) next[L][i] = -1; end[L++] = 0; return L - 1; } void init() { L = 0; root = newnode(); return; } void insert(int* in, int id) { int now = root; for(; *in != -1; in++) { if(next[now][*in] == -1) next[now][*in] = newnode(); now = next[now][*in]; } end[now] = id;//用end[node]表示节点node结尾的数组串的编号 } void build() { fail[root] = root; queue <int> Q; Q.push(root); while(!Q.empty()) { int now = Q.front(); Q.pop(); for(int i = 0; i < CHARSET; i++) if(next[now][i] == -1) next[now][i] = now == root ? root : next[fail[now]][i]; else { fail[next[now][i]] = now == root ? root : next[fail[now]][i]; Q.push(next[now][i]); } } return; } void query(int* in) { int now = root; memset(vis, 0, sizeof(vis)); for(; *in != -1; in++) { now = next[now][*in]; int tmp = now; while(tmp != root) { if(end[tmp]) vis[end[tmp]] = 1; tmp = fail[tmp]; } } int res = 0; for(int i = 1; i <= n; i++) if(vis[i]) res++; printf("%d\n", res); } }; void init() { for(int i = 0; i <= 25; i++) M[i + 'A'] = i; for(int i = 26; i <= 51; i++) M[i - 26 + 'a'] = i; for(int i = 52; i <= 61; i++) M[i - 52 + '0'] = i; M['+'] = 62; M['/'] = 63; M['='] = -1;//M['=']是为了转码的方便 return; } void change() { int len = strlen(s); int vnow = 0; int snow = 0; int t[5]; /* * 容易观察到base64码长度都一定是4的倍数,所以4个一组处理 * =号的出现一定是4个没有,或者3个字符一个等号,或者两个字符一个等号 */ for(; snow + 3 < len; snow += 4) { for(int i = 1; i <= 4; i++) t[i] = M[s[snow + i - 1]]; v[vnow++] = (t[1] << 2) + (t[2] >> 4); if(t[3] != -1) v[vnow++] = ((t[2] % ( 1 << 4)) << 4) + (t[3] >> 2); if(t[4] != -1) v[vnow++] = ((t[3] % ( 1 << 2)) << 6) + t[4]; } v[vnow] = -1;//结尾标记 return; } Trie AC; int main() { init(); int m; while(~scanf("%d", &n)) { AC.init(); for(int i = 1; i <= n; i++) { scanf("%s", s); change(); AC.insert(v, i); } AC.build(); scanf("%d", &m); while(m--) { scanf("%s", s); change(); AC.query(v); } printf("\n"); } return 0; }