Portal: http://http://acm.hdu.edu.cn/showproblem.php?pid=2222
Train of thought: the introduction of AC automata, directly on the AC automaton.
There are only three things we need to do to build an AC automaton:
1) Building a dictionary tree
2) Build failure pointers
3) Build Trie diagram (This problem does not seem to do this step also can a ... But this step does not have to be stuck into O (n^2) ... )
1) The first step is better understood
The root is the virtual root, the edge represents the letter, then the root to terminate the path of the node is a string, so for the same prefix of the string we can save the public prefix space.
Add a pattern string, we are in the trie tree step by step, without the son on the new, or the original side of the walk.
2) The second step is a bit more complicated.
Learning AC automatic KMP is the basis of the reason here. The fail pointer is the next array of KMP in the upgraded version when there are multiple pattern strings
The next array of KMP refers to the length of the substring in the same pattern string, which is both the longest prefix and the suffix, and is defined so as to facilitate jumping when a match fails
and the Fail pointer is also useful for jumps when a match fails. Because it is multiple strings, the fail pointer of a point points to the same suffix of the paragraph, and the other string prefix, so that the jump is achieved when the match fails. Drawing a picture is more intuitive
Then it is to find the fail pointer. To find the next array, we can jump from the previous next array to get the current next array, and the fail pointer is similar.
We can use BFS to find the fail pointer one layer at a level.
1. The fail of the root node points to null, and the fail of the child node of the root node points to the root (because the first bit does not match, of course, to start again)
2. Finish the fail and put it on the tail of the team.
3. Remove the Team head node X, and ask all of its sons ch[x][i] the fail pointer
If Ch[x][i] exists, then fail[ch[x][i]]=ch[fai[x]][i] (in fact should always jump to fail until Ch[x][i] exists, this is not to build trie map will be stuck to O (n^2) reason, the non-existent son to mend, Just one jump.)
Otherwise ch[x][i]=ch[fail[x]][i] (in fact, is the third step to build a trie map, all the non-existent son to make up, save every time to skip a lot of fail)
4. Repeat 2 and 3 until the queue is empty
In this way, the AC automaton is built
Then there's the code ...
#include <cstdio> #include <cstring> #include <algorithm>const int maxn=1000010,maxm=250010;using namespace Std;int cas,n;char s[maxn];struct ac_dfa{int tot,ch[maxm][26],fai[maxm],sum[maxm],q[maxm],head,tail; void Clear () {Tot=0,memset (ch,0,sizeof (CH)), memset (Fai,0,sizeof (FAI));} void Insert () {int p=0; for (int i=1;s[i];p =ch[p][s[i]-' a '],i++) if (!ch[p][s[i]-' a ']) ch[p][s[i]-' a ']=++tot; sum[p]++; } void Getfail () {head=0,q[tail=1]=0,fai[0]=-1; while (head!=tail) {int x=q[++head]; for (int i=0;i<26;i++) if (Ch[x][i]) {q[++tail]=ch[x][i]; int p=x==0?0:ch[fai[x]][i]; while (P!=-1&&!ch[p][i]) p=fai[p]; Fai[ch[x][i]]=p; } else ch[x][i]=x==0?0:ch[fai[x]][i]; }} void Work () {int ans=0; for (int i=1,p=0;s[i];i++) {while (p&&!ch[p][s[i]-' a ']) p=fai[p]; P=ch[p][s[i]-' a ']; for (int t=p;t;t=fai[t]) ans+=sum[t],sum[t]=0;//printf ("%d%d\n", T,sum[t]),} printf ("%d\n", ans); }}t;int Main () {scanf ("%d", &cas); while (cas--) {scanf ("%d", &n); T.clear (); for (int i=1;i<=n;i++) scanf ("%s", s+1), T.insert (); T.getfail (), scanf ("%s", s+1), T.work (); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu2222 Keywords Search & ac Automata Learning Summary