1. 程式人生 > >神奇的字串匹配:擴充套件KMP演算法

神奇的字串匹配:擴充套件KMP演算法

## 引言 一個算是冷門的演算法(在競賽上),不過其演算法思想值得深究。 ## 前置知識 1. **kmp**的演算法**思想**,具體可以參考 → [Click here](https://www.cnblogs.com/RioTian/p/12686870.html) 2. **trie樹(字典樹)**。 ## 正文 **問題定義:**給定兩個字串 S 和 T(長度分別為 n 和 m),下標從 0 開始,定義 `extend[i]` 等於 `S[i]...S[n-1]` 與 T 的最長相同字首的長度,求出所有的 `extend[i]`。舉個例子,看下錶: | i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | :-------: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | | S | a | a | a | a | a | b | b | b | | T | a | a | a | a | a | c | | | | extend[i] | 5 | 4 | 3 | 2 | 1 | 0 | 0 | 0 | 為什麼說這是 KMP 演算法的擴充套件呢?顯然,如果在 S 的若干個位置 i 有 `extend[i]` 等於 m,則可知在 S 中找到了匹配串 T,並且匹配的首位置是 i,這就是標準的KMP問題。但是,擴充套件 KMP 演算法可以找到 S 中所有 T 的匹配。接下來具體介紹下這個演算法。 ### 演算法流程 (1) ![](https://gitee.com//riotian/blogimage/raw/master/img/20201005135842.png) 如上圖,假設當前遍歷到 S 串位置 i,即 `extend[0]...extend[i - 1]`這 i 個位置的值已經計算得到。設定兩個變數,a 和 p。p 代表以 a 為起始位置的字元匹配成功的最右邊界,也就是 "p = 最後一個匹配成功位置 + 1"。相較於字串 T 得出,**S[a...p) 等於 T[0...p-a)**。 再定義一個輔助陣列 `int next[]`,其中 `next[i]` 含義為:`T[i]...T[m - 1]` 與 T 的最長相同字首長度,m 為串 T 的長度。舉個例子: | i | 0 | 1 | 2 | 3 | 4 | 5 | | :-----: | :--: | :--: | :--: | :--: | :--: | :--: | | T | a | a | a | a | a | c | | next[i] | 6 | 4 | 3 | 2 | 1 | 0 | (2) ![](https://gitee.com//riotian/blogimage/raw/master/img/20201005135840.png) `S[i]` 對應 `T[i - a]`,如果 `i + next[i - a] < p`,如上圖,三個橢圓長度相同,根據 next 陣列的定義,此時 `extend[i] = next[i - a]`。 (3) ![](https://gitee.com//riotian/blogimage/raw/master/img/20201005135850.png) 如果 `i + next[i - a] == p` 呢?如上圖,三個橢圓都是完全相同的,`S[p] != T[p - a]` 且 `T[p - i] != T[p - a]`,但 `S[p]` 有可能等於 `T[p - i]`,所以我們可以直接從 `S[p]` 與 `T[p - i]` 開始往後匹配,加快了速度。 (4) ![](https://gitee.com//riotian/blogimage/raw/master/img/20201005135854.png) 如果 `i + next[i - a] > p` 呢?那說明 `S[i...p)` 與 `T[i-a...p-a)` 相同,注意到 `S[p] != T[p - a]` 且 `T[p - i] == T[p - a]`,也就是說 `S[p] != T[p - i]`,所以就沒有繼續往下判斷的必要了,我們可以直接將 `extend[i]` 賦值為 `p - i`。 (5)最後,就是求解 next 陣列。我們再來看下 `next[i]` 與 `extend[i]` 的定義: - **next[i]**: `T[i]...T[m - 1]` 與 T 的最長相同字首長度; - **extend[i]**: `S[i]...S[n - 1]` 與 T 的最長相同字首長度。 恍然大悟,求解 `next[i]` 的過程不就是 T 自己和自己的一個匹配過程嘛,下面直接看程式碼。 ### 程式碼 ```c++ #include #include using namespace std; //求解 T 中 next[],註釋參考 GetExtend() void GetNext(string& T, int& m, int next[]) { int a = 0, p = 0; next[0] = m; for (int i = 1; i < m; ++i) if (i >= p || i + next[i - a] >= p) { if (i >= p) p = i; while (p < m && T[p] == T[p - i]) p++; next[i] = p - i; a = i; } else next[i] = next[i - a]; } // 求解 extend[] void GetExtend(string& S, int& n, string& T, int& m, int extend[], int next[]) { int a = 0, p = 0; GetNext(T, m, next); for (int i = 0; i < n; ++i) { // i >
= p 的作用:舉個典型例子,S 和 T 無一字元相同 if (i >= p || i + next[i - a] >= p) { if (i >= p) p = i; while (p < n && p - i < m && S[p] == T[p - i]) p++; extend[i] = p - i; a = i; } else extend[i] = next[i - a]; } } int main() { int next[100], extend[100]; string S, T; int n, m; while (cin >> S >> T) { int n = S.length(); int m = T.length(); GetExtend(S, n, T, m, extend, next); // 列印 next cout << "next: "; for (int i = 0; i < m; ++i) cout << next[i] << " "; // 列印 extend cout << "\nextend: "; for (int i = 0; i < n; i++) cout << extend[i] << " "; cout << endl << endl; } return 0; } ``` 資料測試如下: ```plaintext aaaaabbb aaaaac next: 6 4 3 2 1 0 extend: 5 4 3 2 1 0 0 0 abc def next: 3 0 0 extend: 0 0 0 ``` ## 參考 OI Wiki:https://oi-wiki.org/string/z-func/ 拓展kmp演算法總結:https://blog.csdn.net/dyx404514/article/details/41831947