您的位置:首页 > 其它

hdu 2222 AC自动机模板题

2014-04-05 13:22 435 查看
首先学KMP   推荐《算法导论》以及本人的KMP博文 http://blog.csdn.net/u011026968/article/details/10382659

在学Trie  这个其实不难,随意找点资料就行

然后开始学AC自动机  http://www.cppblog.com/mythit/archive/2014/03/09/80633.html#206110  这个博文的讲解很好 但是看评论似乎有bug?我自己找的我队友写的模板,然后做点改动作为我自己的模板了

上模版:
/*************************************************/
//AC 自动机 by Pilgrim
//
//MAXLEN 模式串的长度
//str 模式串(待匹配的)
//keyword 待输入的单词
//cnt是否为该单词的最后一个节点,Insert的时候,
//当单词插入完成,其最后一个节点的cnt=1
//root的fail为NULL
//
// 初始化
//root=cur=Trie;
//head = tail = 0;
//root->clr();
//另外在Insert的时候 创建节点的时候也是要clr()的
/*************************************************/

#define MAXLEN 1000010
#define MAXTRIE 500010
#define WORDLEN 51
#define KIND 26

char str[MAXLEN],keyword[WORDLEN];

struct Node{
Node *fail;
Node *next[KIND]; /*next数组里存的是当前节点的孩子*/
int cnt;
void clr()
{
fail = NULL;
cnt = 0;
memset(next,0,sizeof(next)*KIND);
}
}Trie[MAXTRIE],*q[MAXTRIE],*root,*cur; /*看最开头的注释*/
int head,tail;/*队列首尾 初始化head = tail = 0*/

void Insert(char s[]) /*向Tries 插入单词*/
{
int idx,i,n=strlen(s);
Node *p=root;
for(int i=0;i<n;i++)
{
idx = s[i]-'a';
if(p->next[idx]==NULL){
p->next[idx]=++cur;
p->next[idx]->clr();
}
p=p->next[idx];
}
p->cnt++; /*插入完成*/
}

void Build_AC()
{
Node *p,*tmp;
root->fail=NULL;////
q[tail++]=root;
while(head!=tail)
{
p=q[head++];
for(int i=0;i<KIND;i++)
{
if(p->next[i])
{
q[tail++]=p->next[i];
if(p == root)
{
p->next[i]->fail = root;
}
else
{
tmp=p->fail;
while(tmp!=NULL)
{
if(tmp->next[i]) /*tmp->next[i] p->next[i] i都表示'a'+i故如果tmp->next[i]!=NULL,说明以前出现过'a'+i*/
{
p->next[i]->fail=tmp->next[i];
break;
}
tmp=tmp->fail;
}
if(tmp == NULL)
p->next[i]->fail = root;
}
}
}
}
}

//int Query(char str[])
int Query()
{
int ans=0,n=strlen(str),idx;
Node *tmp,*p=root;
for(int i=0;i<n;i++)
{
idx=str[i]-'a';
while(p->next[idx]==NULL && p!=root) //跳转失败指针
p=p->fail;
p=p->next[idx];
if(p==NULL)
p=root;
tmp = p; //p不动,tmp计算后缀串
while(tmp!=root && tmp->cnt!=-1)/*理解的还有些问题*/
{
ans+=tmp->cnt;
tmp->cnt=-1;
tmp=tmp->fail;//指针移向下个字符继续匹配
}
}
return ans;
}

void Init()
{
cur = root = Trie;
root->clr();
head = tail = 0;
}


然后hdu 2222直接套模版

/*******************************************************/
//hdu 2222 AC自动机 by Pilgrim
/******************************************************/

#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;

#define MAXLEN 1000010
#define MAXTRIE 500010
#define WORDLEN 51
#define KIND 26

char str[MAXLEN],keyword[WORDLEN];

struct Node{
Node *fail;
Node *next[KIND]; /*next数组里存的是当前节点的孩子*/
int cnt;
void clr()
{
fail = NULL;
cnt = 0;
memset(next,0,sizeof(next)*KIND);
}
}Trie[MAXTRIE],*q[MAXTRIE],*root,*cur; /*看最开头的注释*/
int head,tail;/*队列首尾 初始化head = tail = 0*/

void Insert(char s[]) /*向Tries 插入单词*/
{
int idx,i,n=strlen(s);
Node *p=root;
for(int i=0;i<n;i++)
{
idx = s[i]-'a';
if(p->next[idx]==NULL){
p->next[idx]=++cur;
p->next[idx]->clr();
}
p=p->next[idx];
}
p->cnt++; /*插入完成*/
}

void Build_AC()
{
Node *p,*tmp;
root->fail=NULL;////
q[tail++]=root;
while(head!=tail)
{
p=q[head++];
for(int i=0;i<KIND;i++)
{
if(p->next[i])
{
q[tail++]=p->next[i];
if(p == root)
{
p->next[i]->fail = root;
}
else
{
tmp=p->fail;
while(tmp!=NULL)
{
if(tmp->next[i]) /*tmp->next[i] p->next[i] i都表示'a'+i故如果tmp->next[i]!=NULL,说明以前出现过'a'+i*/
{
p->next[i]->fail=tmp->next[i];
break;
}
tmp=tmp->fail;
}
if(tmp == NULL)
p->next[i]->fail = root;
}
}
}
}
}

//int Query(char str[])
int Query()
{
int ans=0,n=strlen(str),idx;
Node *tmp,*p=root;
for(int i=0;i<n;i++)
{
idx=str[i]-'a';
while(p->next[idx]==NULL && p!=root) //跳转失败指针
p=p->fail;
p=p->next[idx];
if(p==NULL)
p=root;
tmp = p; //p不动,tmp计算后缀串
while(tmp!=root && tmp->cnt!=-1)/*理解的还有些问题*/
{
ans+=tmp->cnt;
tmp->cnt=-1;
tmp=tmp->fail;//指针移向下个字符继续匹配
}
}
return ans;
}

void Init()
{
cur = root = Trie;
root->clr();
head = tail = 0;
}

int main()
{
int ncase,n;
scanf("%d",&ncase);
while(ncase--)
{
Init();
scanf("%d",&n);

while(n--)
{
scanf("%s",keyword);
Insert(keyword);
}
Build_AC();
scanf("%s",str);
printf("%d\n",Query());
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: