Topic Portal
1 /*2 Test Instructions: Number of occurrences of each text string3 AC Automata: Entry questions, notice the repetition of keywords and the number of occurrences of the keywords are not counted in the total number4 */5 /************************************************6 * Author:running_time7 * Created time:2015-8-14 14:14:328 * File Name:AC.cpp9 ************************************************/Ten One#include <cstdio> A#include <algorithm> -#include <iostream> -#include <sstream> the#include <cstring> -#include <cmath> -#include <string> -#include <vector> +#include <queue> -#include <deque> +#include <stack> A#include <list> at#include <map> -#include <Set> -#include <bitset> -#include <cstdlib> -#include <ctime> - using namespacestd; in - #defineLson L, Mid, RT << 1 to #defineRson mid + 1, R, RT << 1 | 1 +typedefLong Longll; - Const intMAXN = 1e4 +Ten; the Const intMaxnode = MAXN * -+Ten; * Const intINF =0x3f3f3f3f; $ Const intSigma_size = -;Panax Notoginseng Const intMOD = 1e9 +7; - structAC { the intCh[maxnode][sigma_size], F[maxnode], sz; + intVal[maxnode]; A voidInitvoid) { thememset (ch[0],0,sizeof(ch[0])); +SZ =1; val[0] =0; - } $ intIdxCharc) { $ returnC'a'; - } - voidInsertChar*s) { the intU =0; - for(intI=0; S[i]; ++i) {Wuyi intc =idx (s[i]); the if(!Ch[u][c]) { -memset (Ch[sz],0,sizeof(Ch[sz])); WuVAL[SZ] =0; -CH[U][C] = sz++; About } $U =Ch[u][c]; - } -val[u]++; - } A voidBuildvoid) { +queue<int> Q; f[0] =0; the for(intI=0; i<sigma_size; ++i) { - intU = ch[0][i]; $ if(u) { theF[u] =0; Q.push (u); the } the Elsech[0][i] =0; the } - while(!Q.empty ()) { in intU =Q.front (); Q.pop (); the for(intI=0; i<sigma_size; ++i) { the int&v =Ch[u][i]; About if(!v) { thev = ch[f[u]][i];Continue; the } the Q.push (v); +F[V] =Ch[f[u]][i]; - } the }Bayi } the intQueryChar*T) { the intRET =0;intj =0; - for(intI=0; T[i]; ++i) { - intc =idx (t[i]); thej =Ch[j][c]; the intU =J; the while(u) { theRET + = Val[u]; Val[u] =0; -U =F[u]; the } the } the returnret;94 } the }ac; the Charp[ -], t[1000010]; the 98 intMainvoid) {//hdoj 2222 Keywords Search About intT scanf ("%d", &T); - while(t--) {101 intN scanf ("%d", &n);102 ac.init ();103 for(intI=1; i<=n; ++i) {104scanf ("%s", p); Ac.insert (p); the }106Ac.build (); scanf ("%s", T);107printf ("%d\n", Ac.query (t));108 }109 the return 0;111}
AC Automaton hdoj 2222 Keywords Search