题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串。
经典AC自动机模板题,不多说。
1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 #include <string.h> 5 #define MAX 26 6 7 using namespace std; 8 9 struct node 10 { 11 node *fail, *next[MAX]; 12 int count; 13 }Mem_Pool[500001], *Trie_Queue[500001]; 14 static int Used_Node; 15 static char pattern[10001], target[1000001]; 16 17 struct node *Get_New_Node(void); 18 void Insert(struct node*); 19 void Build_AC_Automation(struct node* const); 20 static int Query(struct node* const); 21 22 int main(void) 23 { 24 int case_sum, pattern_sum; 25 scanf("%d", &case_sum); 26 27 while (case_sum--) 28 { 29 Used_Node = 0; 30 node *root = Get_New_Node();//每一次都拿一个新的根 31 scanf("%d", &pattern_sum); 32 getchar(); 33 while (pattern_sum--) 34 Insert(root); 35 Build_AC_Automation(root); 36 gets(target); 37 printf("%d ", Query(root)); 38 } 39 } 40 41 struct node *Get_New_Node(void) 42 { 43 Mem_Pool[Used_Node].count = 0; 44 Mem_Pool[Used_Node].fail = NULL; 45 memset(Mem_Pool[Used_Node].next, 0, sizeof(struct node *)*MAX); 46 47 return &Mem_Pool[Used_Node++]; 48 } 49 50 void Insert(struct node *root) 51 { 52 gets(pattern); 53 54 node *p = root; 55 int index; 56 for (int i = 0; pattern[i] != '