1、AC自动机
1 void buildTrie(Node *root, char *str){ 2 Node *p = root; 3 for(int i = 0; str[i]; ++i){ 4 int index = str[i] - '0'; 5 if(!p->next[index]) p->next[index] = new Node(ecnt++); 6 p = p->next[index]; 7 } 8 p->flag = true; 9 } 10 11 void makeFair(Node *root){ 12 queue<Node*> que; que.push(root); 13 while(!que.empty()){ 14 Node *tmp = que.front(); que.pop(); 15 for(int i = 0; i < 10; ++i){ 16 if(!tmp->next[i]) continue; 17 if(tmp == root) tmp->next[i]->fail = root; 18 else{ 19 Node *p = tmp->fail; 20 while(p){ 21 if(p->next[i]){ 22 tmp->next[i]->fail = p->next[i]; 23 break; 24 } 25 p = p->fail; 26 } 27 if(!p) tmp->next[i]->fail = root; 28 } 29 if(tmp->next[i]->fail->flag) tmp->next[i]->flag = true; 30 else que.push(tmp->next[i]); 31 } 32 } 33 root->fail = root; 34 }