Note that the target string contains characters that are not uppercase letters. Therefore, you must judge each matching process. If it is not an uppercase letter, continue, note that the P pointer should be directed to the root node. Nothing else, that is, the template AC automatic machine. By the way, there is also a question that involves multiple input groups ...... WA's teenagers are hurrying to get rid of it .. The visual test will be passed .. The delete node is updated and the memory is released. If you do not release the memory for a question, MLE is performed. Therefore, the code above is added to facilitate template sorting.
# Include <iostream> # include <cstdio> # include <algorithm> # include <string> # include <cmath> # include <cstring> # include <queue> # include <set> # include <vector> # include <stack> # include <map> # include <iomanip> # define PI acos (-1.0) # define Max 2505 # define inf 1 <28 # define LL (x) (x <1) # define RR (x) (x <1 | 1) # define REP (I, s, t) for (int I = (s); I <= (t); ++ I) # define ll long Long # define mem (a, B) memset (a, B, sizeof (a) # define mp (a, B) make_pair (a, B) # define PII pair <int, int> using namespace std; struct node {node * fail; node * next [26]; int count; int id; node () {fail = 0; count = 0; mem (next, 0); id = 0 ;}} * qe [5000005]; node * root = 0; // insert a []. void insert (char * a, int id) {node * p = root; int l = strlen (a); for (int I = 0; I <l; I ++) {int Tt = a [I]-'A'; if (p-> next [tt] = 0) {p-> next [tt] = new node ();} p = p-> next [tt];} p-> count ++; p-> id = id;} // build * fail. void build () {root-> fail = 0; int h = 0, t = 0; qe [h ++] = root; while (h> t) {node * temp = qe [t ++]; node * p = 0; for (int I = 0; I <26; I ++) {if (temp-> next [I]! = 0) {if (temp = root) temp-> next [I]-> fail = root; else {p = temp-> fail; while (p! = 0) {if (p-> next [I]! = 0) {temp-> next [I]-> fail = p-> next [I]; // find the matching break;} p = p-> fail ;} if (p = 0) temp-> next [I]-> fail = root; // if not found, point fail to root} qe [h ++] = temp-> next [I] ;}}} int anss [1111]; int search (char *) {int l = strlen (a); node * p = root; int ans = 0; for (int I = 0; I <l; I ++) {int tt = a [I]-'A'; if (tt <0 | tt> = 26) {p = root; continue ;} while (p-> next [tt] = 0 & p! = Root) p = p-> fail; p = p-> next [tt]; p = (p = 0 )? Root: p; node * temp = p; while (temp! = Root & temp-> count! = 0) {ans + = temp-> count; anss [temp-> id] ++; // temp-> count =-1; temp = temp-> fail;} return ans;} char a [1111] [55]; char B [2000005]; void deleteAll (node * p) {for (int I = 0; I <26; I ++) {if (p-> next [I]! = 0) {deleteAll (p-> next [I]) ;}} delete p ;}int main () {int n; while (cin> n) {mem (anss, 0); root = new node (); for (int I = 1; I <= n; I ++) {scanf ("% s ", a [I]); insert (a [I], I);} build (); scanf ("% s", B); search (B ); for (int I = 1; I <= n; I ++) {if (anss [I]) {cout <a [I] <": "<anss [I] <endl ;}} deleteAll (root);} return 0 ;}# include <iostream ># include <cstdio> # Include <algorithm> # include <string> # include <cmath> # include <cstring> # include <queue> # include <set> # include <vector> # include <stack> # include <map> # include <iomanip> # define PI acos (-1.0) # define Max 2505 # define inf 1 <28 # define LL (x) (x <1) # define RR (x) (x <1 | 1) # define REP (I, s, t) for (int I = (s); I <= (t); ++ I) # define ll long # define mem (a, B) memset (a, B, sizeof (a) # define mp (, B) make_pair (a, B) # define PII pair <int, int> using namespace std; struct node {node * fail; node * next [26]; int count; int id; node () {fail = 0; count = 0; mem (next, 0); id = 0 ;}} * qe [5000005]; node * root = 0; // insert a []. void insert (char * a, int id) {node * p = root; int l = strlen (a); for (int I = 0; I <l; I ++) {int tt = a [I]-'A'; if (p-> next [tt] = 0) {p-> next [tt] = new Node ();} p = p-> next [tt];} p-> count ++; p-> id = id;} // build * fail. void build () {root-> fail = 0; int h = 0, t = 0; qe [h ++] = root; while (h> t) {node * temp = qe [t ++]; node * p = 0; for (int I = 0; I <26; I ++) {if (temp-> next [I]! = 0) {if (temp = root) temp-> next [I]-> fail = root; else {p = temp-> fail; while (p! = 0) {if (p-> next [I]! = 0) {temp-> next [I]-> fail = p-> next [I]; // find the matching break;} p = p-> fail ;} if (p = 0) temp-> next [I]-> fail = root; // if not found, point fail to root} qe [h ++] = temp-> next [I] ;}}} int anss [1111]; int search (char *) {int l = strlen (a); node * p = root; int ans = 0; for (int I = 0; I <l; I ++) {int tt = a [I]-'A'; if (tt <0 | tt> = 26) {p = root; continue ;} while (p-> next [tt] = 0 & P! = Root) p = p-> fail; p = p-> next [tt]; p = (p = 0 )? Root: p; node * temp = p; while (temp! = Root & temp-> count! = 0) {ans + = temp-> count; anss [temp-> id] ++; // temp-> count =-1; temp = temp-> fail;} return ans;} char a [1111] [55]; char B [2000005]; void deleteAll (node * p) {for (int I = 0; I <26; I ++) {if (p-> next [I]! = 0) {deleteAll (p-> next [I]) ;}} delete p ;}int main () {int n; while (cin> n) {mem (anss, 0); root = new node (); for (int I = 1; I <= n; I ++) {scanf ("% s ", a [I]); insert (a [I], I);} build (); scanf ("% s", B); search (B ); for (int I = 1; I <= n; I ++) {if (anss [I]) {cout <a [I] <": "<anss [I] <endl ;}} deleteAll (root);} return 0 ;}