{"id":80592,"date":"2026-03-05T14:56:58","date_gmt":"2026-03-05T06:56:58","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/80592.html"},"modified":"2026-03-05T14:56:58","modified_gmt":"2026-03-05T06:56:58","slug":"%e3%80%90%e6%96%b0%e6%89%8b%e5%90%91%e3%80%91%e7%90%86%e8%a7%a3kmp%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/80592.html","title":{"rendered":"\u3010\u65b0\u624b\u5411\u3011\u7406\u89e3KMP\u7b97\u6cd5"},"content":{"rendered":"<h4>\u524d\u8a00<\/h4>\n<p>\u672c\u6587\u4ec5\u4f5c\u4e3a\u5c0f\u767d\u5b66\u4e60\u7b97\u6cd5\u65f6\u7684\u590d\u4e60\u3002<\/p>\n<hr \/>\n<h4>\u4e00\u3001 \u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981KMP&#xff1f;<\/h4>\n<p>\u5728\u8bb2\u89e3KMP\u4e4b\u524d&#xff0c;\u6211\u4eec\u5148\u770b\u770b\u666e\u901a\u4eba\u662f\u600e\u4e48\u505a\u5b57\u7b26\u4e32\u5339\u914d\u7684&#xff08;\u4e5f\u5c31\u662f\u66b4\u529b\u7834\u89e3\u6cd5 Brute Force&#xff09;\u3002<\/p>\n<p>\u5047\u8bbe\u8981\u5728\u4e3b\u4e32 text &#061; &#034;ABABCABAA&#034; \u4e2d\u627e\u6a21\u5f0f\u4e32 pattern &#061; &#034;ABACA&#034;\u3002<\/p>\n<p>\u66b4\u529b\u6cd5\u7684\u903b\u8f91\u662f&#xff1a;\u4e3b\u4e32\u548c\u6a21\u5f0f\u4e32\u4e00\u4e2a\u5b57\u7b26\u4e00\u4e2a\u5b57\u7b26\u5bf9\u6bd4&#xff0c;\u5982\u679c\u5339\u914d\u5931\u8d25&#xff0c;\u4e3b\u4e32\u5c31\u9000\u56de\u5230\u521a\u624d\u5f00\u59cb\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;\u6a21\u5f0f\u4e32\u9000\u56de\u5230\u8d77\u70b9&#xff0c;\u91cd\u65b0\u5f00\u59cb\u5bf9\u6bd4\u3002<\/p>\n<p>\u66b4\u529b\u6cd5\u7684\u75db\u70b9&#xff1a;\u6bcf\u6b21\u5339\u914d\u5931\u8d25&#xff0c;\u4e3b\u4e32\u7684\u6307\u9488\u90fd\u8981\u201c\u5012\u8f66\u201d&#xff08;\u56de\u6eaf&#xff09;\u3002\u5982\u679c\u9047\u5230\u5f88\u591a\u91cd\u590d\u7684\u5b57\u7b26&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u98d9\u5347\u5230 O(m x n)\u3002<\/p>\n<p>KMP\u7b97\u6cd5\u7684\u8bde\u751f&#xff0c;\u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u201c\u5012\u8f66\u201d\u95ee\u9898\u3002 \u5b83\u7684\u6838\u5fc3\u601d\u60f3\u662f&#xff1a;\u4e3b\u4e32\u7684\u6307\u9488\u6c38\u8fdc\u4e0d\u56de\u6eaf&#xff0c;\u6bcf\u6b21\u5339\u914d\u5931\u8d25\u65f6&#xff0c;\u5229\u7528\u5df2\u7ecf\u5339\u914d\u8fc7\u7684\u4fe1\u606f&#xff0c;\u8ba9\u6a21\u5f0f\u4e32\u5c3d\u53ef\u80fd\u591a\u5730\u5f80\u53f3\u6ed1\u52a8\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u76f4\u63a5\u964d\u5230\u4e86 O(m &#043; n)&#xff01;<\/p>\n<hr \/>\n<h4>\u4e8c\u3001 KMP\u7684\u7075\u9b42&#xff1a;\u6700\u957f\u516c\u5171\u524d\u540e\u7f00<\/h4>\n<p>\u8981\u8ba9\u6a21\u5f0f\u4e32\u77e5\u9053\u6bcf\u6b21\u5339\u914d\u5931\u8d25\u540e\u8be5\u5f80\u53f3\u6ed1\u52a8\u591a\u5c11&#xff0c;\u6211\u4eec\u9700\u8981\u5f15\u5165KMP\u7684\u7075\u9b42\u6982\u5ff5\u2014\u2014\u524d\u540e\u7f00\u3002<\/p>\n<ul>\n<li>\n<p>\u524d\u7f00&#xff1a;\u5305\u542b\u9996\u5b57\u6bcd&#xff0c;\u4f46\u4e0d\u5305\u542b\u5c3e\u5b57\u6bcd\u7684\u6240\u6709\u5b50\u4e32\u3002<\/p>\n<\/li>\n<li>\n<p>\u540e\u7f00&#xff1a;\u5305\u542b\u5c3e\u5b57\u6bcd&#xff0c;\u4f46\u4e0d\u5305\u542b\u9996\u5b57\u6bcd\u7684\u6240\u6709\u5b50\u4e32\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4ee5\u5b57\u7b26\u4e32 &#034;ABABA&#034; \u4e3a\u4f8b&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u524d\u7f00\u6709&#xff1a;A, AB, ABA, ABAB<\/p>\n<\/li>\n<li>\n<p>\u540e\u7f00\u6709&#xff1a;A, BA, ABA, BABA<\/p>\n<\/li>\n<li>\n<p>\u5b83\u7684\u6700\u957f\u516c\u5171\u524d\u540e\u7f00\u5c31\u662f &#034;ABA&#034;&#xff0c;\u957f\u5ea6\u4e3a 3\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd9\u6709\u4ec0\u4e48\u7528\u5462&#xff1f;<\/p>\n<p>\u5047\u5982\u6211\u4eec\u5728\u4e3b\u4e32\u4e2d\u5339\u914d\u5230\u4e86 &#034;ABABA&#034;&#xff0c;\u4f46\u4e0b\u4e00\u4e2a\u5b57\u7b26\u5339\u914d\u5931\u8d25\u4e86\u3002\u56e0\u4e3a\u6211\u4eec\u77e5\u9053 &#034;ABABA&#034; \u7684\u524d 3 \u4e2a\u5b57\u7b26\u548c\u540e 3 \u4e2a\u5b57\u7b26\u662f\u4e00\u6a21\u4e00\u6837\u7684&#xff08;\u90fd\u662f &#034;ABA&#034;&#xff09;\u3002\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u4ece\u5934\u518d\u6765&#xff0c;\u76f4\u63a5\u628a\u6a21\u5f0f\u4e32\u5f00\u5934\u7684 &#034;ABA&#034; \u5bf9\u9f50\u5230\u521a\u624d\u5339\u914d\u7684\u672b\u5c3e &#034;ABA&#034; \u5c31\u53ef\u4ee5\u4e86&#xff01;<\/p>\n<hr \/>\n<h4>\u4e09\u3001 \u63ed\u79d8 next \u6570\u7ec4&#xff08;\u524d\u7f00\u8868&#xff09;<\/h4>\n<p>\u4e3a\u4e86\u8bb0\u5f55\u6a21\u5f0f\u4e32\u4e2d\u6bcf\u4e2a\u4f4d\u7f6e\u7684\u201c\u6700\u957f\u516c\u5171\u524d\u540e\u7f00\u957f\u5ea6\u201d&#xff0c;\u6211\u4eec\u628a\u5b83\u5b58\u8fdb\u4e00\u4e2a\u6570\u7ec4\u91cc&#xff0c;\u8fd9\u4e2a\u6570\u7ec4\u5728KMP\u4e2d\u901a\u5e38\u88ab\u79f0\u4e3a next \u6570\u7ec4&#xff08;\u6216\u8005\u53eb lps \/ \u524d\u7f00\u8868&#xff09;\u3002<\/p>\n<p>\u5bf9\u4e8e\u6a21\u5f0f\u4e32 pattern &#061; &#034;ABABC&#034;&#xff0c;\u5b83\u7684 next \u6570\u7ec4\u8ba1\u7b97\u5982\u4e0b&#xff1a;<\/p>\n<table>\n<tr>\n<td>\u7d22\u5f15 i<\/td>\n<td>\u5b50\u4e32<\/td>\n<td>\u6700\u957f\u516c\u5171\u524d\u540e\u7f00<\/td>\n<td>\u957f\u5ea6 (next[i])<\/td>\n<\/tr>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>A<\/td>\n<td>\u65e0<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>AB<\/td>\n<td>\u65e0<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>ABA<\/td>\n<td>A<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>ABAB<\/td>\n<td>AB<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>ABABC<\/td>\n<td>\u65e0<\/td>\n<td>0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5339\u914d\u89c4\u5219&#xff1a;<\/p>\n<p>\u5f53\u6211\u4eec\u5728\u67d0\u4e2a\u5b57\u7b26\u5904\u5339\u914d\u5931\u8d25\u65f6&#xff0c;\u770b\u5b83\u524d\u4e00\u4e2a\u5b57\u7b26\u7684 next \u6570\u7ec4\u503c\u3002\u8fd9\u4e2a\u503c\u662f\u51e0&#xff0c;\u6a21\u5f0f\u4e32\u7684\u6307\u9488\u5c31\u8df3\u5230\u7b2c\u51e0\u4e2a\u5b57\u7b26\u7684\u4f4d\u7f6e&#xff08;\u7d22\u5f15\u4e3a\u51e0\u7684\u4f4d\u7f6e&#xff09;&#xff0c;\u800c\u4e3b\u4e32\u6307\u9488\u4fdd\u6301\u4e0d\u52a8&#xff01;<\/p>\n<h4>\u56fe\u89e3\u5730\u72f1\u7ea7\u96be\u70b9&#xff1a;next\u6570\u7ec4\u7684 while \u56de\u9000\u903b\u8f91<\/h4>\n<p>KMP\u7b97\u6cd5\u4e2d&#xff0c;\u6700\u8ba9\u65b0\u624b\u6293\u72c2\u7684\u5c31\u662f\u6784\u5efa next \u6570\u7ec4\u65f6\u7684\u8fd9\u4e09\u884c\u4ee3\u7801&#xff1a;<\/p>\n<p>while (j &gt; 0 &amp;&amp; pattern[i] !&#061; pattern[j]) {<br \/>\n    j &#061; next[j &#8211; 1];<br \/>\n}<\/p>\n<p>\u201c\u4e3a\u4ec0\u4e48\u662f j &#061; next[j-1]&#xff1f;\u600e\u4e48\u4e0d\u662f j-1&#xff1f;\u8fd9\u5230\u5e95\u662f\u5728\u5e72\u561b&#xff1f;\u201d \u522b\u6025&#xff0c;\u6211\u4eec\u7528\u4e00\u5f20\u56fe\u89e3\u628a\u5b83\u6252\u5f97\u5e72\u5e72\u51c0\u51c0&#xff01;<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u7684\u6a21\u5f0f\u4e32\u662f pattern &#061; &#034;ABACA&#034;&#xff0c;\u6211\u4eec\u6b63\u5728\u8ba1\u7b97\u5b83\u7684 next \u6570\u7ec4\u3002<\/p>\n<p>\u524d\u60c5\u63d0\u8981&#xff1a;<\/p>\n<ul>\n<li>\n<p>i \u4ee3\u8868\u5f53\u524d\u6b63\u5728\u5904\u7406\u7684\u5b57\u7b26\u540e\u7f00\u672b\u5c3e\u3002<\/p>\n<\/li>\n<li>\n<p>j \u4ee3\u8868\u5f53\u524d\u5df2\u7ecf\u5339\u914d\u4e0a\u7684\u6700\u957f\u516c\u5171\u524d\u7f00\u7684\u957f\u5ea6&#xff08;\u540c\u65f6\u4e5f\u6307\u5411\u524d\u7f00\u7684\u4e0b\u4e00\u4e2a\u5b57\u7b26&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h5>\u573a\u666f\u8fd8\u539f&#xff1a;\u5f53 i &#061; 3 \u65f6\u53d1\u751f\u4e86\u4ec0\u4e48&#xff1f;<\/h5>\n<p>\u524d\u9762\u51e0\u6b65\u6211\u4eec\u5df2\u7ecf\u7b97\u51fa\u4e86 next \u6570\u7ec4\u7684\u524d\u4e09\u4e2a\u503c&#xff1a;next[0]&#061;0, next[1]&#061;0, next[2]&#061;1\u3002 \u6b64\u65f6&#xff0c;\u6307\u9488 i \u8d70\u5230\u4e86\u7d22\u5f15 3&#xff0c;\u6307\u9488 j \u5728\u7d22\u5f15 1\u3002<\/p>\n<p>\u7d22\u5f15:    0   1   2   3   4<br \/>\n\u6a21\u5f0f\u4e32:  A   B   A   C   A<br \/>\n             ^       ^<br \/>\n           j&#061;1     i&#061;3<\/p>\n<p>\u7b2c\u4e00\u6b65&#xff1a;\u5bf9\u6bd4\u6a21\u5f0f\u4e32 pattern[i] \u548c \u6a21\u5f0f\u4e32pattern[j] \u6211\u4eec\u53d1\u73b0 pattern[3] \u662f &#039;C&#039;&#xff0c;\u800c pattern[1] \u662f &#039;B&#039;\u3002 &#039;C&#039; !&#061; &#039;B&#039;&#xff0c;\u5339\u914d\u5931\u8d25&#xff01;<\/p>\n<p>\u8fd9\u8bf4\u660e\u4ec0\u4e48&#xff1f;\u8bf4\u660e\u5305\u542b &#039;C&#039; \u7684\u5b50\u4e32 &#034;ABAC&#034;&#xff0c;\u5b83\u7684\u6700\u957f\u516c\u5171\u524d\u540e\u7f00\u65e0\u6cd5\u5728\u4e0a\u4e00\u6bb5\u7684\u57fa\u7840\u4e0a\u7ee7\u7eed\u589e\u52a0\u4e86\u3002<\/p>\n<p>\u7b2c\u4e8c\u6b65&#xff1a;\u89e6\u53d1 while \u5faa\u73af&#xff0c;\u6267\u884c j &#061; next[j &#8211; 1] \u65e2\u7136\u6ca1\u6cd5\u589e\u52a0&#xff0c;\u90a3\u6211\u4eec\u662f\u4e0d\u662f\u8981\u628a j \u76f4\u63a5\u6e05\u96f6&#xff0c;\u4ece\u5934\u5f00\u59cb\u7b97&#xff1f; \u5343\u4e07\u4e0d\u8981&#xff01;\u8fd9\u4e5f\u662fKMP\u6700\u806a\u660e\u7684\u5730\u65b9\u3002<\/p>\n<p>\u6211\u4eec\u867d\u7136\u6ca1\u6cd5\u5339\u914d\u8fd9\u4e48\u957f\u7684&#xff0c;\u4f46\u6211\u4eec\u53ef\u4ee5\u201c\u9000\u800c\u6c42\u5176\u6b21\u201d&#xff0c;\u627e\u627e\u770b\u6709\u6ca1\u6709\u66f4\u77ed\u4e00\u70b9\u7684\u516c\u5171\u524d\u540e\u7f00\u3002<\/p>\n<p>\u600e\u4e48\u627e&#xff1f;\u53bb\u627e\u51fa\u9519\u4f4d\u7f6e j \u7684\u524d\u4e00\u4e2a\u5b57\u7b26\u7684 next \u503c\u3002\u4e5f\u5c31\u662f next[j &#8211; 1]\u3002 \u6b64\u65f6 j &#061; 1&#xff0c;\u6240\u4ee5\u6211\u4eec\u53bb\u770b next[0]\u3002 \u67e5\u8868\u5f97\u77e5 next[0] &#061; 0\u3002<\/p>\n<p>\u6240\u4ee5&#xff0c;j \u88ab\u66f4\u65b0\u4e3a 0\u3002<\/p>\n<p>\u7b2c\u4e09\u6b65&#xff1a;\u56de\u9000\u540e\u7684\u91cd\u65b0\u6bd4\u8f83 \u6307\u9488 j \u56de\u9000\u5230\u4e86\u7d22\u5f15 0 \u7684\u4f4d\u7f6e&#xff0c;\u800c i \u4f9d\u7136\u5728\u7d22\u5f15 3 \u6b7b\u6b7b\u76ef\u4f4f &#039;C&#039;\u3002<\/p>\n<p>\u7d22\u5f15:    0   1   2   3   4<br \/>\n\u6a21\u5f0f\u4e32:  A   B   A   C   A<br \/>\n         ^           ^<br \/>\n       j&#061;0         i&#061;3<\/p>\n<p>\u6211\u4eec\u518d\u6b21\u5bf9\u6bd4 pattern[3] \u548c pattern[0]&#xff1a; &#039;C&#039; \u4f9d\u7136\u4e0d\u7b49\u4e8e &#039;A&#039;&#xff01; \u6b64\u65f6 j \u5df2\u7ecf\u662f 0 \u4e86&#xff0c;\u9000\u65e0\u53ef\u9000&#xff08;while \u5faa\u73af\u6761\u4ef6 j &gt; 0 \u88ab\u6253\u7834&#xff09;\u3002<\/p>\n<p>\u7ed3\u5c40&#xff1a; \u65e2\u7136\u9000\u65e0\u53ef\u9000&#xff0c;\u8bf4\u660e\u5b8c\u5168\u6ca1\u6709\u516c\u5171\u524d\u540e\u7f00\u3002\u6240\u4ee5\u8bb0\u5f55\u4e0b next[3] &#061; 0\u3002\u7136\u540e i \u7ee7\u7eed\u5b89\u5fc3\u5f80\u4e0b\u8d70\u3002<\/p>\n<hr \/>\n<h5>\u6838\u5fc3\u603b\u7ed3&#xff1a;\u4ec0\u4e48\u662f j &#061; next[j-1]&#xff1f;<\/h5>\n<p>\u7528\u4e00\u53e5\u5927\u767d\u8bdd\u6765\u8bf4&#xff1a;<\/p>\n<p>j &#061; next[j-1] \u7684\u672c\u8d28\u5c31\u662f&#xff1a;\u5728\u8fd9\u4e2a\u8f83\u957f\u7684\u540e\u7f00\u4e2d\u5339\u914d\u5931\u8d25\u4e86&#xff0c;\u6211\u5c31\u53bb\u201c\u524d\u7f00\u7684\u524d\u7f00\u201d\u91cc\u9762\u627e\u627e&#xff0c;\u770b\u770b\u6709\u6ca1\u6709\u66f4\u77ed\u7684\u80fd\u5339\u914d\u4e0a&#xff01;<\/p>\n<p>\u8fd9\u5c31\u597d\u6bd4\u4fc4\u7f57\u65af\u5957\u5a03&#xff0c;\u6700\u5916\u5c42\u88c5\u4e0d\u8fdb\u53bb\u4e86&#xff0c;\u6211\u4eec\u5c31\u6253\u5f00\u770b\u4e0b\u4e00\u5c42\u80fd\u4e0d\u80fd\u88c5\u8fdb\u53bb&#xff0c;\u76f4\u5230\u9000\u5230\u6700\u6838\u5fc3&#xff08;j&#061;0&#xff09;\u4e3a\u6b62\u3002<\/p>\n<hr \/>\n<h4>\u56db\u3001 \u6838\u5fc3\u4ee3\u7801\u5b9e\u73b0 (C&#043;&#043;\u7248)<\/h4>\n<p>\u7406\u89e3\u4e86\u539f\u7406&#xff0c;\u4ee3\u7801\u5176\u5b9e\u5c31\u662f\u987a\u7406\u6210\u7ae0\u7684\u4e8b\u60c5\u3002\u8fd9\u91cc\u7ed9\u51fa\u5e26\u6709\u8be6\u7ec6\u6ce8\u91ca\u7684\u4ee3\u7801&#xff0c;\u5927\u5bb6\u53ef\u4ee5\u7ed3\u5408\u6ce8\u91ca\u518d\u8fc7\u4e00\u904d\u903b\u8f91&#xff1a;<\/p>\n<p>C&#043;&#043;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>\/\/ \u6b65\u9aa4 1&#xff1a;\u6784\u5efa next \u6570\u7ec4 (\u524d\u7f00\u8868)<br \/>\nvector&lt;int&gt; getNext(string pattern) {<br \/>\n    int m &#061; pattern.length();<br \/>\n    vector&lt;int&gt; next(m, 0); \/\/ \u521d\u59cb\u5316 next \u6570\u7ec4\u5168\u4e3a 0<\/p>\n<p>    int j &#061; 0; \/\/ j \u8868\u793a\u6700\u957f\u516c\u5171\u524d\u540e\u7f00\u7684\u957f\u5ea6&#xff0c;\u4e5f\u8868\u793a\u5f53\u524d\u8981\u5339\u914d\u7684\u524d\u7f00\u7684\u672b\u5c3e<br \/>\n    for (int i &#061; 1; i &lt; m; i&#043;&#043;) { \/\/ i \u4ece 1 \u5f00\u59cb&#xff0c;\u8868\u793a\u5f53\u524d\u5b50\u4e32\u7684\u672b\u5c3e<br \/>\n        \/\/ \u5982\u679c\u524d\u540e\u7f00\u4e0d\u76f8\u540c&#xff0c;j \u9700\u8981\u56de\u9000&#xff0c;\u56de\u9000\u5230 next[j-1] \u7684\u4f4d\u7f6e<br \/>\n        while (j &gt; 0 &amp;&amp; pattern[i] !&#061; pattern[j]) {<br \/>\n            j &#061; next[j &#8211; 1];<br \/>\n        }<br \/>\n        \/\/ \u5982\u679c\u524d\u540e\u7f00\u76f8\u540c&#xff0c;j \u5f80\u524d\u8d70\u4e00\u6b65<br \/>\n        if (pattern[i] &#061;&#061; pattern[j]) {<br \/>\n            j&#043;&#043;;<br \/>\n        }<br \/>\n        \/\/ \u8bb0\u5f55\u5f53\u524d\u4f4d\u7f6e\u7684 next \u503c<br \/>\n        next[i] &#061; j;<br \/>\n    }<br \/>\n    return next;<br \/>\n}<\/p>\n<p>\/\/ \u6b65\u9aa4 2&#xff1a;KMP \u5339\u914d\u8fc7\u7a0b<br \/>\nint kmpSearch(string text, string pattern) {<br \/>\n    int n &#061; text.length();<br \/>\n    int m &#061; pattern.length();<br \/>\n    if (m &#061;&#061; 0) return 0;<\/p>\n<p>    vector&lt;int&gt; next &#061; getNext(pattern); \/\/ \u83b7\u53d6 next \u6570\u7ec4<\/p>\n<p>    int j &#061; 0; \/\/ j \u8d1f\u8d23\u626b\u63cf pattern<br \/>\n    for (int i &#061; 0; i &lt; n; i&#043;&#043;) { \/\/ i \u8d1f\u8d23\u626b\u63cf text&#xff0c;\u6c38\u8fdc\u4e0d\u56de\u9000&#xff01;<br \/>\n        \/\/ \u5982\u679c\u5339\u914d\u5931\u8d25&#xff0c;j \u6839\u636e next \u6570\u7ec4\u8fdb\u884c\u56de\u9000<br \/>\n        while (j &gt; 0 &amp;&amp; text[i] !&#061; pattern[j]) {<br \/>\n            j &#061; next[j &#8211; 1];<br \/>\n        }<br \/>\n        \/\/ \u5982\u679c\u5b57\u7b26\u5339\u914d\u6210\u529f&#xff0c;j \u5f80\u4e0b\u8d70<br \/>\n        if (text[i] &#061;&#061; pattern[j]) {<br \/>\n            j&#043;&#043;;<br \/>\n        }<br \/>\n        \/\/ \u5982\u679c j \u8d70\u5230\u4e86 pattern \u7684\u672b\u5c3e&#xff0c;\u8bf4\u660e\u627e\u5230\u4e86\u5b8c\u6574\u7684\u6a21\u5f0f\u4e32<br \/>\n        if (j &#061;&#061; m) {<br \/>\n            return i &#8211; m &#043; 1; \/\/ \u8fd4\u56de\u5339\u914d\u6210\u529f\u7684\u8d77\u59cb\u7d22\u5f15<br \/>\n        }<br \/>\n    }<br \/>\n    return -1; \/\/ \u6ca1\u627e\u5230\u8fd4\u56de -1<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    string text &#061; &#034;ABABCABAA&#034;;<br \/>\n    string pattern &#061; &#034;ABACA&#034;;<br \/>\n    int index &#061; kmpSearch(text, pattern);<\/p>\n<p>    if (index !&#061; -1) {<br \/>\n        cout &lt;&lt; &#034;\u5339\u914d\u6210\u529f&#xff01;\u8d77\u59cb\u7d22\u5f15\u4e3a: &#034; &lt;&lt; index &lt;&lt; endl;<br \/>\n    } else {<br \/>\n        cout &lt;&lt; &#034;\u5339\u914d\u5931\u8d25&#xff01;&#034; &lt;&lt; endl;<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<hr \/>\n<h4>\u4e94\u3001 \u603b\u7ed3<\/h4>\n<p>KMP\u7b97\u6cd5\u7684\u96be\u70b9\u5176\u5b9e\u5168\u5728 next \u6570\u7ec4\u7684\u63a8\u5bfc\u548c\u56de\u9000\u903b\u8f91 \u4e0a\u3002\u53ea\u8981\u4f60\u5f04\u61c2\u4e86\u201c\u6700\u957f\u516c\u5171\u524d\u540e\u7f00\u201d\u7684\u6982\u5ff5&#xff0c;\u77e5\u9053\u4e3b\u4e32\u6307\u9488 i \u4e0d\u56de\u5934\u3001\u6a21\u5f0f\u4e32\u6307\u9488 j \u6839\u636e next \u6570\u7ec4\u627e\u6377\u5f84&#xff0c;KMP \u5c31\u4e0d\u518d\u662f\u96be\u5543\u7684\u9aa8\u5934\u4e86&#xff01;<\/p>\n<p>\u4f5c\u4e3a\u65b0\u624b\u5c0f\u767d&#xff0c;\u8fd9\u4e5f\u662f\u6211\u5b66\u4e60KMP\u7684\u4e00\u70b9\u5fc3\u5f97\u3002\u5982\u679c\u4f60\u89c9\u5f97\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u5e2e\u52a9&#xff0c;\u6b22\u8fce\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8bc4\u8bba\u533a\u4ea4\u6d41&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00\u672c\u6587\u4ec5\u4f5c\u4e3a\u5c0f\u767d\u5b66\u4e60\u7b97\u6cd5\u65f6\u7684\u590d\u4e60\u3002\u4e00\u3001 \u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981KMP&#xff1f;\u5728\u8bb2\u89e3KMP\u4e4b\u524d&#xff0c;\u6211\u4eec\u5148\u770b\u770b\u666e\u901a\u4eba\u662f\u600e\u4e48\u505a\u5b57\u7b26\u4e32\u5339\u914d\u7684&#xff08;\u4e5f\u5c31\u662f\u66b4\u529b\u7834\u89e3\u6cd5 Brute Force&#xff09;\u3002\u5047\u8bbe\u8981\u5728\u4e3b\u4e32 text  \\&#8221;ABABCABAA\\&#8221; \u4e2d\u627e\u6a21\u5f0f\u4e32 pattern  \\&#8221;ABACA\\&#8221;\u3002\u66b4\u529b\u6cd5\u7684\u903b\u8f91\u662f&#xff1a;\u4e3b\u4e32\u548c\u6a21\u5f0f\u4e32\u4e00\u4e2a\u5b57\u7b26\u4e00\u4e2a\u5b57\u7b26\u5bf9\u6bd4&#xff0c;\u5982\u679c\u5339\u914d\u5931\u8d25&#xff0c;\u4e3b\u4e32\u5c31\u9000\u56de\u5230\u521a\u624d\u5f00\u59cb\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;\u6a21\u5f0f\u4e32\u9000\u56de<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9059,190],"topic":[],"class_list":["post-80592","post","type-post","status-publish","format-standard","hentry","category-server","tag-kmp","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u65b0\u624b\u5411\u3011\u7406\u89e3KMP\u7b97\u6cd5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/80592.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u65b0\u624b\u5411\u3011\u7406\u89e3KMP\u7b97\u6cd5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u524d\u8a00\u672c\u6587\u4ec5\u4f5c\u4e3a\u5c0f\u767d\u5b66\u4e60\u7b97\u6cd5\u65f6\u7684\u590d\u4e60\u3002\u4e00\u3001 \u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981KMP&#xff1f;\u5728\u8bb2\u89e3KMP\u4e4b\u524d&#xff0c;\u6211\u4eec\u5148\u770b\u770b\u666e\u901a\u4eba\u662f\u600e\u4e48\u505a\u5b57\u7b26\u4e32\u5339\u914d\u7684&#xff08;\u4e5f\u5c31\u662f\u66b4\u529b\u7834\u89e3\u6cd5 Brute Force&#xff09;\u3002\u5047\u8bbe\u8981\u5728\u4e3b\u4e32 text &quot;ABABCABAA&quot; \u4e2d\u627e\u6a21\u5f0f\u4e32 pattern &quot;ABACA&quot;\u3002\u66b4\u529b\u6cd5\u7684\u903b\u8f91\u662f&#xff1a;\u4e3b\u4e32\u548c\u6a21\u5f0f\u4e32\u4e00\u4e2a\u5b57\u7b26\u4e00\u4e2a\u5b57\u7b26\u5bf9\u6bd4&#xff0c;\u5982\u679c\u5339\u914d\u5931\u8d25&#xff0c;\u4e3b\u4e32\u5c31\u9000\u56de\u5230\u521a\u624d\u5f00\u59cb\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;\u6a21\u5f0f\u4e32\u9000\u56de\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/80592.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-03-05T06:56:58+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/80592.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/80592.html\",\"name\":\"\u3010\u65b0\u624b\u5411\u3011\u7406\u89e3KMP\u7b97\u6cd5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-03-05T06:56:58+00:00\",\"dateModified\":\"2026-03-05T06:56:58+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/80592.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/80592.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/80592.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u65b0\u624b\u5411\u3011\u7406\u89e3KMP\u7b97\u6cd5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010\u65b0\u624b\u5411\u3011\u7406\u89e3KMP\u7b97\u6cd5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/80592.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u65b0\u624b\u5411\u3011\u7406\u89e3KMP\u7b97\u6cd5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u524d\u8a00\u672c\u6587\u4ec5\u4f5c\u4e3a\u5c0f\u767d\u5b66\u4e60\u7b97\u6cd5\u65f6\u7684\u590d\u4e60\u3002\u4e00\u3001 \u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981KMP&#xff1f;\u5728\u8bb2\u89e3KMP\u4e4b\u524d&#xff0c;\u6211\u4eec\u5148\u770b\u770b\u666e\u901a\u4eba\u662f\u600e\u4e48\u505a\u5b57\u7b26\u4e32\u5339\u914d\u7684&#xff08;\u4e5f\u5c31\u662f\u66b4\u529b\u7834\u89e3\u6cd5 Brute Force&#xff09;\u3002\u5047\u8bbe\u8981\u5728\u4e3b\u4e32 text \"ABABCABAA\" \u4e2d\u627e\u6a21\u5f0f\u4e32 pattern \"ABACA\"\u3002\u66b4\u529b\u6cd5\u7684\u903b\u8f91\u662f&#xff1a;\u4e3b\u4e32\u548c\u6a21\u5f0f\u4e32\u4e00\u4e2a\u5b57\u7b26\u4e00\u4e2a\u5b57\u7b26\u5bf9\u6bd4&#xff0c;\u5982\u679c\u5339\u914d\u5931\u8d25&#xff0c;\u4e3b\u4e32\u5c31\u9000\u56de\u5230\u521a\u624d\u5f00\u59cb\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;\u6a21\u5f0f\u4e32\u9000\u56de","og_url":"https:\/\/www.wsisp.com\/helps\/80592.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-03-05T06:56:58+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/80592.html","url":"https:\/\/www.wsisp.com\/helps\/80592.html","name":"\u3010\u65b0\u624b\u5411\u3011\u7406\u89e3KMP\u7b97\u6cd5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-03-05T06:56:58+00:00","dateModified":"2026-03-05T06:56:58+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/80592.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/80592.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/80592.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u65b0\u624b\u5411\u3011\u7406\u89e3KMP\u7b97\u6cd5"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/80592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=80592"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/80592\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=80592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=80592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=80592"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=80592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}