AC自动机

匿名 (未验证) 提交于 2019-12-03 00:11:01

AC自动机

1.常见的就是给出n个单词,再给出一段包含m个字符的文章,让你找出有多少个单词在文章里出现过。

2.算法分为3步:构造一棵Trie树,构造失败指针和模式匹配过程。简单来说,AC自动机是用来进行多模式匹配(单个主串,多个模式串)的高效算法

题目:

Keywords Search

 

In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.

InputFirst line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
OutputPrint how many keywords are contained in the description.Sample Input

1 5 she he say shr her yasherhs

Sample Output

3题解:1.要用到的结构
struct node {     int cnt;     node *fail;//失败指针      node *next[26]; }*qu[500005];
 

2.进行结点的初始化,建树

void Init(node *root)//结点的初始化  {     root->cnt=0;     root->fail=NULL;     for(int i=0;i<26;i++)         root->next[i]=NULL; } void build(char *keyword)//构建Trie树  {     node *p,*q;     int i,v;     int len=strlen(keyword);     for(i=0,p=root;i<len;i++)     {         v=keyword[i]-'a';         if(p->next[v]==NULL)         {             q=(struct node *)malloc(sizeof(node));             Init(q);             p->next[v]=q;//结点链接          }         p=p->next[v];//指针移动到下一个结点      }     p->cnt++;//单词最后一个结点cnt++,代表一个单词  }

3.构造失败指针

失败指针的作用就是将主串某一位之前的所有可以与模式串匹配的单词快速在Trie树中找出。

这也是trie树与AC自动机的区别

void build_AC(node *root) {     int head=0,tail=0;//队列头、尾指针      qu[head++]=root;//先将root入队      while(head!=tail)     {         node *p=NULL;         node *temp=qu[tail++];          for(int 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;                 }                 qu[head++]=temp->next[i];              }         }     } }

4.模式匹配

int query(node *root) {      int i,v,count=0;     node *p=root;     int len=strlen(s);     for(i=0;i<len;i++)     {         v=s[i]-'a';         while(p->next[v]==NULL && p!=root)             p=p->fail;         p=p->next[v];         if(p==NULL)//若指针返回为空,则没有找到与之匹配的字符              p=root;         node *temp=p;         while(temp!=root)         {             if(temp->cnt>=0)             {                 count+=temp->cnt;                 temp->cnt=-1;//标记              }             else                 break;             temp=temp->fail;         }     }     return count; }

详细过程参考https://blog.csdn.net/liu940204/article/details/51347064

详细代码

#include <stdio.h> #include <stdlib.h> #include <string.h> struct node {     int cnt;     node *fail;//失败指针      node *next[26]; }*qu[500005]; char s[1000005];//主字符串  char keyword[55];//需要查找的单词  node *root; void Init(node *root)//结点的初始化  {     root->cnt=0;     root->fail=NULL;     for(int i=0;i<26;i++)         root->next[i]=NULL; } void build(char *keyword)//构建Trie树  {     node *p,*q;     int i,v;     int len=strlen(keyword);     for(i=0,p=root;i<len;i++)     {         v=keyword[i]-'a';         if(p->next[v]==NULL)         {             q=(struct node *)malloc(sizeof(node));             Init(q);             p->next[v]=q;//结点链接          }         p=p->next[v];//指针移动到下一个结点      }     p->cnt++;//单词最后一个结点cnt++,代表一个单词  } void build_AC(node *root) {     int head=0,tail=0;//队列头、尾指针      qu[head++]=root;//先将root入队      while(head!=tail)     {         node *p=NULL;         node *temp=qu[tail++];          for(int 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;                 }                 qu[head++]=temp->next[i];              }         }     } } int query(node *root) {      int i,v,count=0;     node *p=root;     int len=strlen(s);     for(i=0;i<len;i++)     {         v=s[i]-'a';         while(p->next[v]==NULL && p!=root)             p=p->fail;         p=p->next[v];         if(p==NULL)//若指针返回为空,则没有找到与之匹配的字符              p=root;         node *temp=p;         while(temp!=root)         {             if(temp->cnt>=0)             {                 count+=temp->cnt;                 temp->cnt=-1;//标记              }             else                 break;             temp=temp->fail;         }     }     return count; } int main() {     int T,n;     scanf("%d",&T);     while(T--)     {         root=(struct node *)malloc(sizeof(node));         Init(root);         scanf("%d",&n);         for(int i=0;i<n;i++)         {             scanf("\n%s",keyword);             build(keyword);         }         build_AC(root);         scanf("\n%s",s);         printf("%d\n",query(root));     }     return 0; }

 

 

 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!