AC自动机

· · 个人记录

在学习 AC 自动机之前,一定要先学会 KMP 算法和 Trie 树(以后有时间再填这个坑吧)。

*** $AC$ 自动机是高效解决多模式匹配的**数据结构**。 利用 $Trie$ 树构建失配指针,达到 $O(n)$ 多模式匹配的目的。 像 $KMP$ 的 $next$ 数组一样,$AC$ 自动机使用 $fail$ 数组来优化失配时的转移过程,$fail$ 即为失配指针。 *** 首先用 $Trie$ 树存储所有的模式串。像这样: ```cpp INPUT: 5 ache headache hehe heache ac ``` ![](https://cdn.luogu.com.cn/upload/pic/20974.png) $tr[i].fail$ 保存一个是当前串(从根到 $i$ 的串)的最大后缀的位置,比如 $headache$ 在 $Trie$ 中的最大后缀为 $ache$ ,那么 $tr[13].fail=5$。 具体的,每次失配时,沿着 $fail$ 转移,找到能够匹配的位置继续匹配。 *** ```cpp #include<cstdio> #include<cmath> #include<iostream> #include<cstring> #include<queue> using namespace std; const int CHARSET_SIZE=26; const char BASH_CHAR='a'; const int N=100005; struct Aho_Corasick_Automation { int CNT=1; struct Trie_Node { int c[CHARSET_SIZE],next,fail; // 表示孩子们、后缀连接、失配指针。 bool isword; // 表示是不是模式串。 int ans; // 表示模式串在文本串中出现的次数。 Trie_Node(bool I=false): ans(0), isword(I), next(0), fail(0) {for(int i=0;i<CHARSET_SIZE;++i) c[i]=0;} } tr[N]; void apply(int cur) { while(tr[cur].next) ++tr[cur].ans, cur=tr[cur].next; ++tr[cur].ans; } Aho_Corasick_Automation() {} int Newnode() { return ++CNT; } int insert(char str[]) // 返回新插入单词的位置。 { int cur=1; int now=0, now_c=0, len=strlen(str); while(now<len) { now_c=str[now]-'a'; if(!tr[cur].c[now_c]) tr[cur].c[now_c]=Newnode(); cur=tr[cur].c[now_c]; ++now; } tr[cur].isword=true; return cur; } void build() { std::queue <int> q; tr[1].fail=1; tr[1].next=0; for(int i=0;i<CHARSET_SIZE;++i) { if(tr[1].c[i]) { q.push(tr[1].c[i]); tr[tr[1].c[i]].fail=1; tr[tr[1].c[i]].next=0; } } register int cur=0,now=0,j; while(!q.empty()) { cur=q.front(); q.pop(); for(int i=0;i<CHARSET_SIZE;++i) { if(now=tr[cur].c[i]) { j=tr[cur].fail; while(j!=1 && !tr[j].c[i]) j=tr[j].fail; tr[now].fail=(tr[j].c[i]? tr[j].c[i]:1); q.push(now); } } tr[cur].next=(tr[tr[cur].fail].isword? tr[cur].fail:tr[tr[cur].fail].next); // 此处并不能加条件判断,否则需要递归。不加条件判断则只需要选择。 } } void exec(const char str[]) { int len=strlen(str), cur=1, now_c=0; for(int i=0;i<len;++i) { now_c=str[i]-'a'; while(cur!=1 && (!tr[cur].c[now_c])) cur=tr[cur].fail; cur=(tr[cur].c[now_c]? tr[cur].c[now_c]:1); if(tr[cur].isword) apply(cur); // 每个串的后缀连接都有可能不为空。 else if(tr[tr[cur].next].isword) apply(tr[cur].next); // 注意是 else if,防止过多的计算。 } } void clear() { for(int i=1;i<=CNT;++i) { tr[i]=Trie_Node(); } CNT=1; } void Debug() // 这个也留着吧。。。 { char C[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; for(int i=1;i<=CNT;++i) { printf("tr[%d]:\nson:",i); for(int j=0;j<13;++j) { printf("%c:%d ",C[j],tr[i].c[j]); }putchar('\n');printf(" "); for(int j=13;j<26;++j) { printf("%c:%d ",C[j],tr[i].c[j]); }putchar('\n'); printf("next=%d fail=%d isword=%d ans=%d\n",tr[i].next,tr[i].fail,tr[i].isword,tr[i].ans); } } }AC_Au; char buf[10000001], *buf_S, *buf_T; char get_char() { if(buf_S==buf_T) { buf_T=(buf_S=buf)+fread(buf,1,10000000,stdin)-1; if(buf_S==buf_T) return EOF; return *buf_S; } return *++buf_S; } #define getchar get_char int read() { char ch=getchar(); int x=0; while(ch<'0' || ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48), ch=getchar(); return x; } void reads(char str[]) { char ch=getchar(); int x=-1; while(ch<'a' || ch>'z') ch=getchar(); // 这句一定要有。。。否则会出现str='\000' 的江局。PS.这是我第一次体会到 \n(Linux换行) 和 \r\n(Windows回车+换行) 的不同。不能保证数据是否是Linux下造的,所以为了保险,加这么一句while()。 while(ch>='a' && ch<='z') str[++x]=ch, ch=getchar(); str[++x]='\000'; // 这句非常的重要!!!这样做既不用每次清空,又能保证串的长度是正确的。 // 如果不加这句,插入和匹配时上一次的串的后半部分也会被考虑到。也就是说:本次匹配的文本串和模式串的长度可能是错的。 // 比如:第一次的文本串为abcdefg,第二次的文本串为aaaa,那么不加这句会导致第二次的文本串为aaaaefg。 } int n; char s[152][72],ss[1000005]; int pos[152]; int Ans; int main() { while(n=read()) { for(int i=1;i<=n;++i) { reads(s[i]); pos[i]=AC_Au.insert(s[i]); } AC_Au.build(); reads(ss); AC_Au.exec(ss); for(int i=1;i<=n;++i) { if(AC_Au.tr[pos[i]].ans>Ans) Ans=AC_Au.tr[pos[i]].ans; } printf("%d\n",Ans); for(int i=1;i<=n;++i) { if(AC_Au.tr[pos[i]].ans==Ans) { printf("%s\n",s[i]); } } Ans=0; AC_Au.clear(); } return 0; } ```