Introduction to AC automata
Aho-corasick automaton, the algorithm, which was produced in Bell Labs in 1975, is one of the famous multimode matching algorithms. Before learning AC automata, there is a basis for trie tree and KMP pattern matching algorithms.
The AC automaton algorithm is divided into 3 steps: 1. Construct a tire Tree 2. Construct failure pointer 3. Pattern matching
Optimization of AC automata: Trie diagram
Keywords Search
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others
Total Submission (s): 38688 Accepted Submission (s): 12473
problem Descriptionin the modern time, Search engine came to the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring the feature to his image retrieval system.
Every image has a long description, when users type some keywords to find the image, the system would match the keywords W ITH 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 would b E match.
InputFirst line would contain one integer means how many cases would follow by.
Each case would contain integers n means the number of keywords and n keywords follow. (N <= 10000)
Each keyword would only contains characters ' a '-' Z ', and the length would be is not longer than 50.
The last line is the description, and the length would be a not longer than 1000000.
OutputPrint How many keywords is contained in the description.
Sample Input
15shehesayshrheryasherhs
Sample Output
3
title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2222
To find the number of occurrences of a given word in an article
Topic Analysis: Naked multi-pattern matching, AC machine template problem
#include <cstdio> #include <cstring> #include <queue>using namespace std;int Const MAX = 1e6 + 5;struct No De//trie tree {int cnt; Number of words node *next[26]; leaf node node *fail; Failed pointer node ()//initialization {cnt = 0; Memset (Next, NULL, sizeof (next)); Fail = NULL; }};char word[51], Text[max];//trie construction void Insert (node *p, char *s) {for (int i = 0; s[i]! = ' + '; i++) {int I DX = s[i]-' a '; if (P-next[idx] = = NULL) P--NEXT[IDX] = new node (); p = P-NEXT[IDX]; } P--cnt + +; Indicates that the word has occurred and saved occurrences}void ac_automation (node *root) {queue <node*> Q;//node queue Q.push (root); Get the Fail pointer while (!q.empty ()) {Node *p = Q.front (); Q.pop (); for (int i = 0; i < i++) {if (P-next[i])//Determine if the node exists {//root The first layer below The failed pointer to the node points to root if (p = = root) p-> Next[i], fail = root; The failure pointer of the current node points to the son node of its failed node, else p, next[i], fail = p, fail, next[i]; Q.push (P-next[i]); } else//trie diagram optimization {if (p = = root) P-next[i] = root; else P-next[i] = p, fail-and next[i]; }}}}int Query (node *root) {int cnt = 0, Len = strlen (text); Node *p = root; for (int i = 0; i < len; i++) {int idx = text[i]-' a '; while (!p-next[idx] && p! = root) p = p, fail; p = P-NEXT[IDX]; if (!p) p = root; Node *tmp = p; while (tmp! = root) {if (TMP-and CNT->= 0) {CNT = tmp--cnt; TMP--cnt =-1; } else break; TMP = TMPFail }} return cnt;} int main () {int T; scanf ("%d", &t); while (t--) {node *root = new node (); int n; scanf ("%d", &n); while (n--) {scanf ("%s", word); Insert (root, Word); } ac_automation (root); scanf ("%s", text); printf ("%d\n", Query (Root)); }}
HDU 2222 Keywords Search (AC Automaton starter template)