ZOJ 3430 Detect the Virus(AC自动机)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3430

题意:给你n个编码后的模式串,和m个编码后的主串,求原来主串中含有模式串的个数

思路:首先要将模式串解码成未编码前来建立ac自动机,然后解码主串扫描统计即可。

code:

 #include <cstdio>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
const int KIND = ;
const int MAXN = ; struct Trie
{
int next[MAXN][KIND], fail[MAXN], id[MAXN];
int root, L, num;
int create()
{
for (int i = ; i < KIND; ++i)
next[L][i] = -;
return L++;
}
void init()
{
L = ;
num = ;
memset(id, , sizeof(id));
root = create();
}
void insert(unsigned char str[], int len)
{
int now = root;
for (int i = ; i < len; ++i)
{
if (- == next[now][str[i]])
next[now][str[i]] = create();
now = next[now][str[i]];
}
id[now] = ++num;
}
void build()
{
queue<int>Q;
fail[root] = root;
for (int i = ; i < KIND; ++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 < KIND; ++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(unsigned char str[], int len)
{
set<int>S;
int now = root;
for (int i = ; i < len; ++i)
{
now = next[now][str[i]];
int temp = now;
while (temp != root)
{
if (id[temp]) S.insert(id[temp]);
temp = fail[temp];
}
}
return (int)S.size();
}
}; Trie ac;
char buf[];
unsigned char temp[];
unsigned char str[]; unsigned char Tran(char ch)
{
if (ch >= 'A' && ch <= 'Z') return ch - 'A';
if (ch >= 'a' && ch <= 'z') return ch - 'a' + ;
if (ch >= '' && ch <= '') return ch - '' + ;
if (ch == '+') return ;
return ;
} int Decode(int len)
{
int k = ;
for (int i = ; i < len; i += )
{
str[k++] = (temp[i]<<)|(temp[i + ]>>);
if (i + < len) str[k++] = (temp[i + ]<<)|(temp[i + ]>>);
if (i + < len) str[k++] = (temp[i + ]<<)|(temp[i + ]);
}
return k;
} int main()
{
int n, m;
while (scanf("%d", &n) != EOF)
{
ac.init();
for (int i = ; i <n; ++i)
{
scanf("%s", buf);
int len = strlen(buf);
while ('=' == buf[len - ]) --len;
for (int j = ; j < len; ++j) temp[j] = Tran(buf[j]);
ac.insert(str, Decode(len));
}
ac.build();
scanf("%d", &m);
for (int i = ; i < m; ++i)
{
scanf("%s", buf);
int len = strlen(buf);
while ('=' == buf[len - ]) --len;
for (int j = ; j < len; ++j) temp[j] = Tran(buf[j]);
printf("%d\n", ac.query(str, Decode(len)));
}
printf("\n");
}
return ;
}
上一篇:Mac环境下Android Studio配置Git以及最基本使用


下一篇:NYOJ题目198数数