1. 程式人生 > >18.10.29 多模式串字符串匹配模板題~AC自動機

18.10.29 多模式串字符串匹配模板題~AC自動機

輸入 queue prev pac 輸出 spl 串匹配 opened ++

描述

給若幹個模式串,以及若幹個句子,判斷每個句子裏是否包含模式串。 句子和模式串都由小寫字母組成

輸入第一行是整數n,表示有n個模式串 ( n <= 1000)
接下來n行每行一個模式串。每個模式串長度不超過20
接下來一行是整數m,表示有m個句子 (m <= 1000)
接下來m行,每行一個句子,每個句子長度不超過1000輸出對每個句子,如果包含某個模式串,則輸出 YES, 否則輸出 NO

樣例輸入

3
abc
def
gh
2
abcd
ak

樣例輸出

YES
NO

來源

Xu Yewen

技術分享圖片
 1 #include <iostream>
 2 #include <string
.h> 3 #include <algorithm> 4 #include <stack> 5 #include <string> 6 #include <math.h> 7 #include <queue> 8 #include <stdio.h> 9 #include <string.h> 10 #include <vector> 11 #include <fstream> 12 #include <set> 13 14 using
namespace std; 15 const int maxn = 1005; 16 char line[maxn]; 17 int m, n,nodecou; 18 struct node { 19 node*next[26]; 20 node*prev; 21 bool isdanger; 22 node() { 23 memset(next, 0, sizeof(next)); 24 prev = NULL; 25 isdanger = false; 26 } 27 }tree[maxn*20
]; 28 29 void build() { 30 for (int i = 0; i < 26; i++) 31 tree[0].next[i] = tree + 1; 32 tree[1].prev = tree; 33 queue<node*>q; 34 q.push(tree+1); 35 while (!q.empty()) { 36 node*now = q.front(); 37 q.pop(); 38 for (int i = 0; i < 26; i++) { 39 node*child = now->next[i]; 40 if (child) { 41 node*prev = now->prev; 42 while (prev->next[i] == NULL) 43 prev = prev->prev; 44 child->prev = prev->next[i]; 45 if (prev->isdanger) 46 child->isdanger = true; 47 q.push(child); 48 } 49 } 50 } 51 } 52 53 void search(char*str) { 54 node*first = tree + 1; 55 for (int i = 0; str[i] != \0; i++) { 56 while (first->next[str[i]-a] == NULL) 57 first = first->prev; 58 if (first->next[str[i]-a]->isdanger) { 59 printf("YES\n"); 60 return; 61 } 62 first = first->next[str[i] - a]; 63 } 64 printf("NO\n"); 65 } 66 67 void init() { 68 scanf("%d", &n); 69 nodecou = 1; 70 for (int i = 1; i <= n; i++) { 71 scanf("%s",line); 72 node *first = tree + 1; 73 int l = strlen(line); 74 for (int i = 0; i!=l ; i++) { 75 if (first->next[line[i] - a] == NULL) { 76 nodecou++; 77 first->next[line[i] - a] = tree + nodecou; 78 } 79 first = first->next[line[i] - a]; 80 if (i == l - 1) 81 first->isdanger = true; 82 } 83 } 84 build(); 85 scanf("%d", &m); 86 while (m--) { 87 scanf("%s", line); 88 search(line); 89 } 90 } 91 92 int main() 93 { 94 init(); 95 return 0; 96 }
View Code

預熱題

18.10.29 多模式串字符串匹配模板題~AC自動機