博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AC自动机
阅读量:4350 次
发布时间:2019-06-07

本文共 11081 字,大约阅读时间需要 36 分钟。

AC自动机

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

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

题目:

 

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

15shehesayshrheryasherhs

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
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
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;}

详细过程参考

详细代码

#include 
#include
#include
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
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
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

 

 

当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻。。。。在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事儿啊~~
但网路上总有那么些网站,开始借着民众的好奇心,打着介绍日食的旗号,大肆传播病毒。小t不幸成为受害者之一。小t如此生气,他决定要把世界上所有带病毒的网站都找出来。当然,谁都知道这是不可能的。小t却执意要完成这不能的任务,他说:“子子孙孙无穷匮也!”(愚公后继有人了)。
万事开头难,小t收集了好多病毒的特征码,又收集了一批诡异网站的源码,他想知道这些网站中哪些是有病毒的,又是带了怎样的病毒呢?顺便还想知道他到底收集了多少带病毒的网站。这时候他却不知道何从下手了。所以想请大家帮帮忙。小t又是个急性子哦,所以解决问题越快越好哦~~

Input第一行,一个整数N(1<=N<=500),表示病毒特征码的个数。

接下来N行,每行表示一个病毒特征码,特征码字符串长度在20—200之间。
每个病毒都有一个编号,依此为1—N。
不同编号的病毒特征码不会相同。
在这之后一行,有一个整数M(1<=M<=1000),表示网站数。
接下来M行,每行表示一个网站源码,源码字符串长度在7000—10000之间。
每个网站都有一个编号,依此为1—M。
以上字符串中字符都是ASCII码可见字符(不包括回车)。
Output依次按如下格式输出按网站编号从小到大输出,带病毒的网站编号和包含病毒编号,每行一个含毒网站信息。
web 网站编号: 病毒编号 病毒编号 …
冒号后有一个空格,病毒编号按从小到大排列,两个病毒编号之间用一个空格隔开,如果一个网站包含病毒,病毒数不会超过3个。
最后一行输出统计信息,如下格式
total: 带病毒网站数
冒号后有一个空格。
Sample Input

3aaabbbccc2aaabbbcccbbaacc

Sample Output

web 1: 1 2 3total: 1
输出病毒编号要用变量标记一下。统计有哪几个病毒出现过用mark数组
#include 
#include
#include
using namespace std;const int maxn=100010;char str[10010],keyword[201],mark[501];int num,cas;struct node{ node *fail; node *next[95]; int cnt,px; node() { fail=NULL; cnt =0; px=0; for(int i=0;i<95;i++) { next[i]=NULL; } }}*q[maxn];node *root;void build(char *s){ int temp,len; node *p=root; for(int i=0;s[i]!='\0';i++) { temp=s[i]-33; if(p->next[temp]==NULL) p->next[temp]=new node(); p=p->next[temp]; } p->cnt++; p->px=num++; }void build_AC(){ q[0]=root; root->fail=NULL; for(int i=0,j=1;i
next[k]) { node *p=now->fail; while(p&&!p->next[k]) p=p->fail; now->next[k]->fail=p?p->next[k]:root; q[j++]=now->next[k]; } } }}int query(){ node *now=root; int ans=0; int flag=0; for(int i=0;str[i];i++) { int in=str[i]-33; while(now&&!now->next[in]) now=now->fail; now=now?now->next[in]:root; for(node *p=now;p!=NULL;p=p->fail) { if(p->cnt==1) { mark[p->px]=1; flag=1; } } } return flag;}int main(){ int n; while(~scanf("%d",&n)) { memset(q,0,sizeof(q)); root=new node(); getchar(); num=1; int temp; temp=n; while(n--) { gets(keyword); build(keyword); } build_AC(); int m; scanf("%d",&m); cas=1; int ans=0; while(m--) { scanf("%s",str); memset(mark,0,sizeof(mark)); int flag=query(); if(flag==1) { printf("web %d:",cas); for(int i=1;i<=temp;i++) if(mark[i]) printf(" %d",i); ans++; printf("\n"); } cas++; } printf("total: %d\n",ans); } return 0;}

 

 
小t非常感谢大家帮忙解决了他的上一个问题。然而病毒侵袭持续中。在小t的不懈努力下,他发现了网路中的“万恶之源”。这是一个庞大的病毒网站,他有着好多好多的病毒,但是这个网站包含的病毒很奇怪,这些病毒的特征码很短,而且只包含“英文大写字符”。当然小t好想好想为民除害,但是小t从来不打没有准备的战争。知己知彼,百战不殆,小t首先要做的是知道这个病毒网站特征:包含多少不同的病毒,每种病毒出现了多少次。大家能再帮帮他吗?
Input第一行,一个整数N(1<=N<=1000),表示病毒特征码的个数。 接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。 在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。 Output按以下格式每行一个,输出每个病毒出现次数。未出现的病毒不需要输出。 病毒特征码: 出现次数 冒号后有一个空格,按病毒特征码的输入顺序进行输出。 Sample Input
3AABBCCooxxCC%dAAAoen....END
Sample Output
AA: 2CC: 1
Hint
Hit:题目描述中没有被提及的所有情况都应该进行考虑。比如两个病毒特征码可能有相互包含或者有重叠的特征码段。计数策略也可一定程度上从Sample中推测。
trie树+kmp 是看这篇
#include 
#include
#include
#include
using namespace std;int n,no;const int maxn = 50010;const int as = 200;const int mm = 2e6+10;char s[mm],ss[1010][51];int ch[maxn][as],fail[maxn],val[maxn],last[maxn],cnt[mm];void insert(int v){ int u = 0,len = strlen(ss[v]); for(int i = 0 ; i < len ; i++){ int tmp = ss[v][i]-'A'; if(!ch[u][tmp]){ memset(ch[no],0,sizeof(ch[no])); val[no] = 0; ch[u][tmp] = no++; } u = ch[u][tmp]; } val[u] = v;}void getfail(){ queue
q; fail[0] = 0; for(int i = 0 ; i < as ; i++){ if(ch[0][i]){ fail[ch[0][i]] = 0; q.push(ch[0][i]); last[ch[0][i]] = 0; } } while(!q.empty()){ int tmp = q.front(); q.pop(); for(int i = 0 ; i < as ; i++){ int u = ch[tmp][i]; if(u != 0){ q.push(u); int v = fail[tmp]; while(v && ch[v][i]== 0) v = fail[v]; fail[u] = ch[v][i]; last[u] = val[fail[u]]?fail[u]:last[fail[u]]; } } }}void cal(int j){ if(j){ cnt[val[j]]++; cal(last[j]); }}void find(){ int len = strlen(s),j = 0; for(int i = 0 ; i < len ; i++){ int tmp = s[i]-'A'; while(j && ch[j][tmp] == 0) j = fail[j]; j = ch[j][tmp]; if(val[j]) cal(j); else if(last[j]) cal(last[j]); }}void init(){ no = 1; memset(ch[0],0,sizeof(ch[0])); memset(cnt, 0, sizeof(cnt[0])*(n+2)); for(int i = 1 ;i <= n ; i++){ scanf("%s",ss[i]); insert(i); } getfail();}int main(){ while(~scanf("%d",&n)){ init(); scanf("%s",s); find(); for(int i = 1; i <= n ; i ++) { if(cnt[i] == 0) continue; printf("%s: %d\n",ss[i],cnt[i]); } } return 0;}

 

 
 

转载于:https://www.cnblogs.com/ylrwj/p/11571121.html

你可能感兴趣的文章
socket创建UDP服务端和客户端
查看>>
Spring笔记--0907
查看>>
ArcGIS Engine中添加点、线、面元素
查看>>
SQL Server 2012安装时如何不安装自带的Visual Studio
查看>>
js判断图片上传格式
查看>>
网络传输协议总结(转载)
查看>>
C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 角色权限的配置页面改进优化...
查看>>
如何编写Spring-Boot自动配置
查看>>
一些开发中不常用但很实用的小技能,可能就一行代码,随时更新,个人备忘...
查看>>
(三)Asp.net web api中的坑-【http post请求中的参数】
查看>>
洛谷跑路
查看>>
使用DbProviderFactories.GetFactory方法需要配置数据库提供者
查看>>
Ubuntu || LinuxMint 配置apache虚拟主机
查看>>
HTML—链接
查看>>
将进程设置为守护进程
查看>>
用连接池提高Servlet访问数据库的效率
查看>>
luogu P1494 [国家集训队]小Z的袜子 ( 普 通 )
查看>>
树的数据结构
查看>>
MyEclipse导入Color Theme
查看>>
Vue开发微信H5 微信分享签名失败问题解决方案
查看>>