# Include <iostream> # include <cstring> # include <cstdio> # include <cstdlib> # include <queue> using namespace std;/** Reference: http://www.cnblogs.com/procedure2012/archive/2012/01/29/2331460.html Thanks: the templates and handouts provided by the lpp senior Pre-Knowledge: http://blog.csdn.net/c0de4fun/article/details/7879597 Tought: Nothing to say is the AC automatic machine ah, Pre-knowledge to the array implementation, dynamic Allocation is implemented here. This mainly refers to the template Query function. */Struct node {node * fail; int count; node * next [26]; node () {fail = NULL; memset (next, 0, sizeof (next )); count = 0 ;}}; const int MAX_SIZE = 1000010; const int MAX_WORD = 62; char dest [MAX_SIZE]; char word [MAX_WORD]; queue <node *> q; void AC_ins (char * word, node * root) {int len = strlen (word); node * tmp = root; for (int I = 0; I <len; I ++) {int k = word [I]-'A'; if (! Tmp-> next [k]) tmp-> next [k] = new node (); tmp = tmp-> next [k];} tmp-> count ++ ;} void AC_build (node * root) {root-> fail = NULL; q. push (root); while (! Q. empty () {node * tmp = q. front (); node * p = NULL; q. pop (); for (int I = 0; I <26; I ++) {if (tmp-> next [I]) {if (tmp = root) {tmp-> next [I]-> fail = root;} else {p = tmp-> fail; while (p) {if (p-> next [I]) {tmp-> next [I]-> fail = p-> next [I]; break;} p = p-> fail;} if (! P) tmp-> next [I]-> fail = root;} q. push (tmp-> next [I]) ;}}} int AC_query (node * root, char * str) {int cnt = 0; node * p = root; int len = strlen (str); for (int I = 0; I <len; I ++) {int ind = str [I]-'A'; while (! P-> next [ind] & p! = Root) p = p-> fail; p = p-> next [ind]; if (p = NULL) p = root; node * tmp = p; while (tmp! = Root & tmp-> count! =-1) {cnt = cnt + tmp-> count; // instead of cnt ++? Let's try; tmp-> count =-1; tmp = tmp-> fail;} return cnt;} int main () {# ifndef ONLINE_JUDGE freopen ("B: \ acm \ SummerVacation \ String-I \ B. in "," r ", stdin); freopen (" B: \ acm \ SummerVacation \ String-I \ B. out "," w ", stdout); # endif int T, N; while (scanf (" % d ", & T )! = EOF) {while (T --) {node * root = new node (); scanf ("% d \ n", & N); for (int I = 0; I <N; I ++) {gets (word); AC_ins (word, root);} AC_build (root); gets (dest); int ans = AC_query (root, dest); printf ("% d \ n", ans) ;}# ifndef ONLINE_JUDGE fclose (stdin); fclose (stdout); # endif return 0 ;}