Chinese question not explained
Sample Input
3 aabbccooxxcc % daaaoen... end
Sample output
AA: 2cc: 1
The number of times the virus appears!
# Include <stdio. h> # include <string. h >#include <queue >#include <algorithm> # include <iostream> using namespace STD; const int kind = 26; const int M = 1005; struct node {node * fail; node * Next [kind]; int count; node () {fail = NULL; Count = 0; memset (next, 0, sizeof (next ));}} * Q [50 * m]; char keyword [1005] [55], STR [2000010]; int head, tail; void insert (char * STR, node * root, int num) {node * P = root; int I = 0, index; while (STR [I]) {inde X = STR [I]-'A'; If (p-> next [Index] = NULL) P-> next [Index] = new node (); P = p-> next [Index]; I ++;} p-> COUNT = num;} void build_ac (node * root) {int I; root-> fail = NULL; Q [head ++] = root; while (Head! = Tail) {node * temp = Q [tail ++]; node * P = NULL; for (I = 0; I <kind; I ++) {If (temp-> next [I]! = NULL) {If (temp = root) temp-> next [I]-> fail = root; // The failure Pointer Points to rootelse {P = temp-> fail; while (P! = NULL) {If (p-> next [I]! = NULL) {temp-> next [I]-> fail = p-> next [I]; break;} p = p-> fail ;} if (P = NULL) temp-> next [I]-> fail = root;} Q [head ++] = temp-> next [I] ;}} int vis [1005]; void query (char * STR, node * root) {int I = 0, CNT = 0, index, Len = strlen (STR ); node * P = root; while (STR [I]) {If (STR [I] <'A' | STR [I]> 'Z') {P = root; I ++; continue;} Index = STR [I]-'A'; while (p-> next [Index] = NULL & P! = Root) P = p-> fail; P = p-> next [Index]; P = (P = NULL )? Root: P; node * temp = P; while (temp! = Root) {vis [temp-> count] ++; temp = temp-> fail;} I ++ ;}} int main () {int n, m; while (~ Scanf ("% d", & N) {head = tail = 0; node * root = new node (); getchar (); For (INT I = 1; I <= N; I ++) {gets (keyword [I]); insert (keyword [I], root, I);} build_ac (Root ); scanf ("% s", STR); memset (VIS, 0, sizeof (VIS); query (STR, root); For (INT I = 1; I <= N; I ++) if (vis [I]) printf ("% s: % d \ n", keyword [I], vis [I]);} return 0 ;}