1. 程式人生 > >字串匹配的Boyer-Moore演算法

字串匹配的Boyer-Moore演算法

# BM演算法介紹 各種文字編輯器的 "查詢" 功能(Ctrl+F),大多采用 Boyer-Moore 演算法。 ![](https://img2020.cnblogs.com/blog/1849509/202003/1849509-20200306153610097-1779530870.png) Boyer-Moore 演算法不僅效率高,而且構思巧妙,容易理解。1977 年,德克薩斯大學的 Robert S. **B**oyer 教授和 J Strother **M**oore 教授發明了一種新的字串匹配演算法:**Boyer-Moore** 演算法,簡稱 **BM 演算法**。 該演算法 **從模式串的尾部開始匹配**,且擁有在最壞情況下 O(N) 的時間複雜度。有資料表明,在實踐中,比 KMP 演算法的實際效能高,可以快大概 3-5 倍。 `BM 演算法中有兩個核心規則:壞字元規則與好字尾規則` # 定義 **BM演算法** 的一個特點是當不匹配的時候 **一次性可以跳過不止一個字元** 。即它不需要對被搜尋的字串中的字元進行逐一比較,而會跳過其中某些部分。通常搜尋關鍵字越長,演算法速度越快。它的效率來自於這樣的事實:對於每一次失敗的匹配嘗試,演算法都能夠使用這些資訊來排除儘可能多的無法匹配的位置。 它充分利用待搜尋字串的 **一些特徵** ,加快了搜尋的步驟。 那它是利用了什麼特性去 **排除儘可能多的無法匹配的位置** 呢? 它是基於以下兩個規則讓模式串每次向右移動 **儘可能大** 的距離。 - 壞字元規則(**bad-character shift**):當文字串中的某個字元跟模式串的某個字元不匹配時,我們稱文字串中的這個失配字元為壞字元,此時模式串需要向右移動,移動的位數 = 壞字元在模式串中的位置 – 壞字元在模式串中最右出現的位置。此外,如果”壞字元”不包含在模式串之中,則最右出現位置為 -1。**壞字元針對的是文字串。** - 好字尾規則(**good-suffix shift**):當字元失配時,後移位數 = 好字尾在模式串中的位置 – 好字尾在模式串上一次出現的位置,且如果好字尾在模式串中沒有再次出現,則為 -1。**好字尾針對的是模式串。** ![01](http://www.cxyxiaowu.com/wp-content/uploads/2019/10/1571057407-9a489061cd17ee2.png) # **壞字元規則** 壞字元出現的時候有兩種情況進行討論。 1、**模式串中沒有出現了文字串中的那個壞字元**,將模式串直接整體對齊到這個字元的後方,繼續比較。 ![02](http://www.cxyxiaowu.com/wp-content/uploads/2019/10/1571057409-78753cc0ff114b9.png) ![](http://www.cxyxiaowu.com/wp-content/uploads/2019/10/1571057410-947564977c44abb.png) 2、模式串中有對應的壞字元時,讓模式串中 **最靠右** 的對應字元與壞字元相對。 這句話有一個關鍵詞是 **最靠右**。 思考一下為什麼是 **最靠右**? 看圖! ![](http://www.cxyxiaowu.com/wp-content/uploads/2019/10/1571057410-8d59a388f3e7175.png) ![](http://www.cxyxiaowu.com/wp-content/uploads/2019/10/1571057411-4fe5562871c4950.png) ![](http://www.cxyxiaowu.com/wp-content/uploads/2019/10/1571057411-1d63d5c08ea8d21.png) ![](http://www.cxyxiaowu.com/wp-content/uploads/2019/10/1571057412-28b14365701e46b.png) ### 實現程式碼 ```cpp //計算壞字元陣列bmBc[] void PreBmBc(char *pattern, int m, int bmBc[]) { int i; for (i = 0; i < 256; ++i) { bmBc[i] = m; } for (i = 0; i < m - 1; ++i) { bmBc[pattern[i]] = m - i - 1; } } ```
# **好字尾規則** Case 1、如果模式串中存在已經匹配成功的好字尾,則把目標串與好字尾對齊,然後從模式串的最尾元素開始往前匹配。 ![](http://www.cxyxiaowu.com/wp-content/uploads/2019/10/1571057412-dcb7f4dd8489c42.png) ![](http://www.cxyxiaowu.com/wp-content/uploads/2019/10/1571057413-44f4ca87ab8fb6f.png) Case 2、如果無法找到匹配好的字尾,找一個匹配的最長的字首,讓目標串與最長的字首對齊(如果這個字首存在的話)。**模式串[m-s,m] = 模式串[0,s]** 。 ![](http://www.cxyxiaowu.com/wp-content/uploads/2019/10/1571057413-f6dc60c28060a1e.png) ![](http://www.cxyxiaowu.com/wp-content/uploads/2019/10/1571057414-806c7080e1cfae0.png) Case 3、如果完全不存在和好字尾匹配的子串,則右移整個模式串。 ```cpp //計算好字尾陣列 void PreBmGs(char *pattern, int m, int bmGs[]) { int i, j; int suff[SIZE]; //SIZE 256 // 計算字尾陣列 suffix(pattern, m, suff); // 先全部賦值為m,包含Case3 for(i = 0; i < m; i++) { bmGs[i] = m; } // Case2 j = 0; for(i = m - 1; i >
= 0; i--) { if(suff[i] == i + 1) { for(; j < m - 1 - i; j++) { if(bmGs[j] == m) bmGs[j] = m - 1 - i; } } } // Case1 for(i = 0; i <= m - 2; i++) { bmGs[m - 1 - suff[i]] = m - 1 - i; } } ``` ```cpp //在計算bmGc陣列時,為提高效率,先計算輔助陣列suff[]表示好字尾的長度。 void suffix(char *pattern, int m, int suff[]) { int i, j; int k; suff[m - 1] = m; for(i = m - 2; i >= 0; i--) { j = i; while(j >= 0 && pattern[j] == pattern[m - 1 - i + j]) j--; suff[i] = i - j; } } ``` **其實還可以再進行優化** ```cpp void suffix(char *pattern, int m, int suff[]) { int f, g, i; suff[m - 1] = m; g = m - 1; for (i = m - 2; i >
= 0; --i) { if (i > g && suff[i + m - 1 - f] < i - g) suff[i] = suff[i + m - 1 - f]; else { if (i < g) g = i; f = i; while (g >= 0 && pattern[g] == pattern[g + m - 1 - f]) --g; suff[i] = f - g; } } } ``` # 完整BM程式碼 ```cpp #include using namespace std; #define MAX_CHAR 256 #define SIZE 256 #define MAX(x,y) (x)>(y)?(x):(y) void BoyerMoore(char *pattern, int m, char *text, int n); int main() { char text[256], pattern[256]; while (1) { scanf("%s%s", text, pattern); if (text == 0 || pattern == 0) break; BoyerMoore(pattern, strlen(pattern), text, strlen(text)); printf("\n"); } return 0; } //計算壞字元陣列bmBc[] void PreBmBc(char *pattern, int m, int bmBc[]) { int i; for (i = 0; i < 256; ++i) { bmBc[i] = m; } for (i = 0; i < m - 1; ++i) { bmBc[pattern[i]] = m - i - 1; } } /* suff陣列的定義:m是pattern的長度 a. suffix[m-1] = m; b. suffix[i] = k for [ pattern[i-k+1] ...,pattern[i]] == [pattern[m-1-k+1],pattern[m-1]] */ void suffix_old(char *pattern, int m, int suff[]) { int i, j; suff[m - 1] = m; for (i = m - 2; i >
= 0; i--) { j = i; while (j >= 0 && pattern[j] == pattern[m - 1 - i + j]) j--; suff[i] = i - j; } } void suffix(char *pattern, int m, int suff[]) { int f, g, i; suff[m - 1] = m; g = m - 1; for (i = m - 2; i >= 0; --i) { if (i > g && suff[i + m - 1 - f] < i - g) suff[i] = suff[i + m - 1 - f]; else { if (i < g) g = i; f = i; while (g >= 0 && pattern[g] == pattern[g + m - 1 - f]) --g; suff[i] = f - g; } } } //計算好字尾陣列bmGs[] void PreBmGs(char *pattern, int m, int bmGs[]) { int i, j; int suff[SIZE]; //計算字尾陣列 suffix(pattern, m, suff); // 先全部賦值為m,包含Case3 for (i = 0; i < m; i++) { bmGs[i] = m; } // Case2 j = 0; for (i = m - 1; i >= 0; i--) { if (suff[i] == i + 1) { for (; j < m - 1 - i; j++) { if (bmGs[j] == m) bmGs[j] = m - 1 - i; } } } // Case1 for (i = 0; i <= m - 2; i++) { bmGs[m - 1 - suff[i]] = m - 1 - i; } } void print(int *array, int n, char *arrayName) { int i; printf("%s: ", arrayName); for (i = 0; i < n; i++) { printf("%d ", array[i]); } printf("\n"); } void BoyerMoore(char *pattern, int m, char *text, int n) { int i, j, bmBc[MAX_CHAR], bmGs[SIZE]; // Preprocessing PreBmBc(pattern, m, bmBc); PreBmGs(pattern, m, bmGs); // Searching j = 0; while (j <= n - m) { for (i = m - 1; i >= 0 && pattern[i] == text[i + j]; i--); if (i < 0) { printf("Find it, the position is %d\n", j+1); j += bmGs[0]; return; } else { j += MAX(bmBc[text[i + j]] - m + 1 + i, bmGs[i]); } } printf("No find.\n"); } ``` Ps:圖片來自網路,侵