题目链接
就是求的是每个病毒出现的次数,可以重叠……嗯,最后别忘了释放内存,不然听说会MLE(但是我比较规矩……不会遇到这种现象的,MLE了一次,就记得要释放内存了)。
是一道AC自动机的模板题。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2e6 + 7;
int N, ans[1005];
char virus[1005][55], sss[maxN];
struct node
{
node *next[26];
node *fail;
int sum, id;
node()
{
memset(next, 0, sizeof(next));
fail = NULL;
sum = id = 0;
}
};
node *root;
void update(char *s, int id)
{
node *temp = root;
int len = (int)strlen(s);
for(int i=0; i<len; i++)
{
int x = s[i] - 'A';
if(temp->next[x] == NULL) temp->next[x] = new node();
temp = temp->next[x];
}
temp->sum++;
temp->id = id;
}
void build_fail()
{
queue<node *> Q;
Q.push(root);
node *p, *temp;
while(!Q.empty())
{
temp = Q.front(); Q.pop();
for(int i=0; i<26; i++)
{
if(temp->next[i])
{
if(temp == root) temp->next[i]->fail = root;
else
{
p = temp->fail;
while(p)
{
if(p->next[i]) { temp->next[i]->fail = p->next[i]; break; }
p = p->fail;
}
if(p == NULL) temp->next[i]->fail = root;
}
Q.push(temp->next[i]);
}
}
}
}
bool check(char s) { return s>='A' && s<='Z'; }
void AC_auto(char *s)
{
node *p = root, *temp;
int len = (int)strlen(s);
for(int i=0; i<len; i++)
{
if(!check(s[i])) { p = root; continue; }
int x = s[i] - 'A';
while(p != root && !p->next[x]) p = p->fail;
p = p->next[x];
if(!p) p = root;
temp = p;
while(temp != root && temp->sum >= 0)
{
if(temp->sum) ans[temp->id]++;
temp = temp->fail;
}
}
}
inline void init()
{
root = (node *)malloc(sizeof(node));
for(int i=0; i<26; i++) root->next[i] = NULL;
root->fail = NULL;
root->sum = 0;
memset(ans, 0, sizeof(ans));
}
inline void FREE(node *temp)
{
for(int i=0; i<26; i++) if(temp->next[i]) FREE(temp->next[i]);
free(temp);
}
int main()
{
while(scanf("%d", &N)!=EOF)
{
init();
for(int i=1; i<=N; i++)
{
scanf("%s", virus[i]);
update(virus[i], i);
}
build_fail();
scanf("%s", sss);
AC_auto(sss);
for(int i=1; i<=N; i++) if(ans[i]) printf("%s: %d\n", virus[i], ans[i]);
FREE(root);
}
return 0;
}
/*
2
AA
AAA
ooxxCC%dAAAoen....ENDooxxCC%dAAAoen....ENDooxxCC%dAAAoen....ENDooxxCC%dAAAoen....END
ans:
AA: 8
AAA: 4
*/
后面再附上静态的加上last优化的模板。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e3 + 7;
int N, tot;
char s[maxN][55], text[2000006];
struct node
{
int nex[26], fail, last, id;
node() { fail = last = id = 0; for(int i=0; i<26; i++) nex[i] = 0; }
}a[maxN * 50];
inline void Insert(int id)
{
int u = 0, len = (int)strlen(s[id]);
for(int i=0, Index; i<len; i++)
{
Index = s[id][i] - 'A';
if(!a[u].nex[Index])
{
a[++tot] = node();
a[u].nex[Index] = tot;
}
u = a[u].nex[Index];
}
a[u].id = id;
}
queue<int> Q;
inline void build_Fail()
{
while(!Q.empty()) Q.pop();
Q.push(0);
int u, v, p;
while(!Q.empty())
{
u = Q.front(); Q.pop();
for(int i=0; i<26; i++)
{
v = a[u].nex[i];
if(v)
{
if(!u) a[u].fail = 0;
else
{
p = a[u].fail;
while(p && !a[p].nex[i]) p = a[p].fail;
a[v].fail = a[p].nex[i];
}
if(a[a[v].fail].id) a[v].last = a[v].fail;
else a[v].last = a[a[v].fail].last;
Q.push(v);
}
}
}
}
int ans[maxN];
inline void Ac_Auto()
{
int len = (int)strlen(text), u = 0, p;
for(int i=0, id; i<len; i++)
{
id = text[i] - 'A';
if(id < 0 || id >= 26) { u = 0; continue; }
while(u && !a[u].nex[id]) u = a[u].fail;
u = a[u].nex[id];
if(!u) continue;
p = u;
while(p)
{
if(a[p].id) ans[a[p].id]++;
p = a[p].last;
}
}
}
inline void init()
{
tot = 0;
a[0] = node();
for(int i=1; i<=N; i++) ans[i] = 0;
}
int main()
{
while(scanf("%d", &N) != EOF)
{
init();
for(int i=1; i<=N; i++)
{
scanf("%s", s[i]);
Insert(i);
}
build_Fail();
scanf("%s", text);
Ac_Auto();
for(int i=1; i<=N; i++)
{
if(!ans[i]) continue;
printf("%s: %d\n", s[i], ans[i]);
}
}
return 0;
}