题意:给出几个模板串,再给出一篇文章,问模板串在文章中出现了几次
思路;ACz自动机模板直接套用。
该模板只适用于26个字母,根据题意要稍加改动
#include <iostream>
#include <algorithm>
#include <cmath>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <queue>
#include <map>
using namespace std;
const int kind = 26;
struct node
{
node* fail; //失败指针
node* next[kind]; //Tire每个节点的个子节点(最多个字母)
int count; //是否为该单词的最后一个节点
node() //构造函数初始化
{
fail = NULL;
count = 0;
memset(next, NULL, sizeof(next));
}
}*q[500001]; //队列,方便用于bfs构造失败指针
char keyword[51]; //输入的单词
char str[1000001]; //模式串
int head,tail; //队列的头尾指针
node* root;
void insert(char* str, node* root) {
node* p = root;
int i = 0, index;
while (str[i])
{
index = str[i] - 'a';
if (p->next[index] == NULL) p->next[index] = new node();
p = p->next[index];
i++;
}
p->count++; //在单词的最后一个节点count+1,代表一个单词
}
void build_ac_automation(node* root) {
int i;
root->fail = NULL;
q[head++] = root;
while (head != tail)
{
node* temp = q[tail++];
node* p = NULL;
for (i = 0; i < 26; i++)
{
if (temp->next[i] != NULL)
{
if (temp == root) temp->next[i]->fail = root;
else
{
p = temp->fail;
while (p != NULL)
{
if (p->next[i] != NULL)
{
temp->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if (p == NULL) temp->next[i]->fail = root;
}
q[head++] = temp->next[i];
}
}
}
}
int query(node* root)
{
int i = 0, cnt = 0, index, len = strlen(str);
node* p = root;
while (str[i])
{
index = str[i] - 'a';
while (p->next[index] == NULL && p != root) p = p->fail;
p = p->next[index];
p = (p == NULL) ? root : p;
node* temp = p;
while (temp != root && temp->count != -1)
{
cnt += temp->count;
temp->count = -1;
temp = temp->fail;
}
i++;
}
return cnt;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
root = new node();
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%s", keyword);
insert(keyword, root);
}
build_ac_automation(root);
scanf("\n%s", str);
printf("%d\n", query(root));
}
return 0;
}
题意:给出若干病毒的特征码,不超过500个。每个病毒的特征码长度在20—200之间。现在有若干网站源代码,要检测网站的源代码中是否包含病毒。网站的个数不超过1000个,每个网站的源代码长度在7000~10000之间。已知如果包含病毒,最多包含三个病毒。输出每个含病毒网站包含的病毒的编号等信息,最后输出含病毒网站的个数。
思路:比较裸的AC自动机的题,我们可以在构造tire树的过程中顺便把编号插入,然后询问时纪录病毒个数的同时用一个数组纪录病毒的编号,然后排序输出即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
#define son_num 130
#define maxn 10010
struct node
{
int code;
int terminal;
node *fail;
node *next[son_num];
node()
{
fail=NULL;
code=0;
terminal=0;
memset(next,NULL,sizeof(next));
}
};
int ans[5]; //纪录主串含有的病毒的编号
//构建Tire树
void insert(node *root,char *str,int x) //x为该病毒的编号
{
node *p=root;
int i=0,index;
while(str[i])
{
index=str[i];
if(p->next[index]==NULL)
p->next[index]=new node();
p=p->next[index];
i++;
}
p->code=x;
p->terminal=1;
}
//寻找失败指针
void build_fail(node *root)
{
queue <node *> que;
root->fail=NULL;
que.push(root);
while(!que.empty())
{
node *temp=que.front();
que.pop();
node *p=NULL;
for(int i=0;i<son_num;i++)
{
if(temp->next[i]!=NULL)
{
if(temp==root) temp->next[i]->fail=root;
else{
p=temp->fail;
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)
temp->next[i]->fail=root;}
que.push(temp->next[i]);
}
}
}
}
//询问主串中含有多少个关键字
int query(node *root,char *str)
{
int i=0,cnt=0,index,len;
len=strlen(str);
node *p=root;
while(str[i])
{
index=str[i];
while(p->next[index]==NULL&&p!=root)
p=p->fail;
p=p->next[index];
if(p==NULL) p=root;
node *temp=p;
while(temp!=root&&temp->code)
{
ans[cnt]=temp->code;
cnt+=p->terminal;
temp=temp->fail;
}
i++;
}
return cnt;
}
int main()
{
int n,m;
char str[205];
char web[maxn];
while(scanf("%d",&n)!=-1)
{
node *root=new node();
for(int i=1;i<=n;i++)
{
scanf("%s",str);
insert(root,str,i);
}
build_fail(root);
int cnt=0; //纪录有多少个网站含有病毒
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%s",web);
bool flag=false; //标记该网站是否含有病毒
int num=query(root,web);
if(num)
{
flag=true;
printf("web %d:",i);
sort(ans,ans+num);
for(int j=0;j<num;j++)
printf(" %d",ans[j]);
printf("\n");
}
if(flag) cnt++;
}
printf("total: %d\n",cnt);
}
return 0;
}