{"id":574,"date":"2026-07-05T18:28:45","date_gmt":"2026-07-05T10:28:45","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=574"},"modified":"2026-07-05T18:40:18","modified_gmt":"2026-07-05T10:40:18","slug":"kmp%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/07\/05\/kmp%e7%ae%97%e6%b3%95\/","title":{"rendered":"KMP\u7b97\u6cd5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u7ed9\u51fa\u4e24\u4e2a\u5b57\u7b26\u4e32 S\uff08\u4e3b\u4e32\uff09\u3001T\u6a21\u5f0f\u4e32\uff09\uff0c\u8f93\u51fa T \u5728 S\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u8d77\u59cb\u4e0b\u6807<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u82e5\u4e0d\u5b58\u5728\u8fd4\u56de -1\u3002\u7b49\u4ef7\u4e8e\u5b9e\u73b0 indexOf\uff0c\u8981\u6c42\u7528 KMP\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>S = \"ababcabcacbab\", T = \"abcac\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>5<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const sInput = \"ababcabcacbab\";\nconst tInput = \"abcac\"\n\nfunction findSubstring(s, t) { \n    const index = s.indexOf(t);\n    console.log(index);\n}\n\nfindSubstring(sInput, tInput);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><mark>\u200c<\/mark>\u91c7\u7528\u66b4\u529b\u6bd4\u5bf9\u65b9\u5f0f\uff0c\u9700\u8981\u5229\u7528\u53cc\u5c42for\u5faa\u73af\u5b9e\u73b0<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>function findSubstring2(s, t){\n    let index = -1;\n    for(let i = 0; i &lt; s.length - t.length + 1; i++){ \n        for(let j = 0; j &lt; t.length; j++){\n            if(s&#91;i+j] !== t&#91;j]){\n                break;\n            }\n            if(j === t.length - 1){\n                index = i;\n            }\n        }\n    }\n    console.log(index);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark><strong>KMP\u7b97\u6cd5\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5b57\u7b26\u4e32\u5339\u914d\u7b97\u6cd5<\/strong>\u200c<\/mark>\uff0c\u6838\u5fc3\u662f\u5229\u7528\u5df2\u5339\u914d\u4fe1\u606f\u51cf\u5c11\u91cd\u590d\u6bd4\u8f83\uff0c\u8ba9\u4e3b\u4e32\u6307\u9488\u4e0d\u56de\u6eaf\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4ece\u66b4\u529b\u5339\u914d\u7684<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><semantics><mrow><mi>O<\/mi><mo stretchy=\"false\">(<\/mo><mi>n<\/mi><mi>m<\/mi><mo stretchy=\"false\">)<\/mo><\/mrow><\/semantics><\/math><em>O<\/em>(<em>nm<\/em>)\u964d\u5230<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><semantics><mrow><mi>O<\/mi><mo stretchy=\"false\">(<\/mo><mi>n<\/mi><mo>+<\/mo><mi>m<\/mi><mo stretchy=\"false\">)<\/mo><\/mrow><\/semantics><\/math>\u3002\u200c\u200c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u7b97\u6cd5\u5230\u5e95\u5728\u5e72\u5565<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c31\u662f\u7ed9\u5b9a\u4e24\u4e2a\u5b57\u7b26\u4e32\uff0c\u5224\u65ad\u5b50\u4e32\u662f\u5426\u5728\u4e3b\u4e32\u4e2d\u51fa\u73b0\uff0c\u5e76\u8fd4\u56de\u51fa\u73b0\u4f4d\u7f6e\u3002\u66b4\u529b\u5339\u914d\u6bcf\u6b21\u4e0d\u5339\u914d\u5c31\u8ba9\u4e3b\u4e32\u6307\u9488\u56de\u9000\u91cd\u65b0\u5f00\u59cb\uff0cKMP\u5219\u8ba9\u4e3b\u4e32\u6307\u9488\u4e00\u76f4\u5411\u524d\u8d70\uff0c\u53ea\u8ba9\u5b50\u4e32\u6307\u9488\u6839\u636e\u5df2\u5339\u914d\u4fe1\u606f\u56de\u9000\u5230\u5408\u9002\u4f4d\u7f6e\u3002\u200c\u200c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9700\u8981\u4e86\u89e3 \u524d\u7f00\u3001\u540e\u7f00\u3001\u6700\u957f\u76f8\u7b49\u524d\u540e\u7f00\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u524d\u7f00<\/strong>\uff1a\u4e0d\u5305\u542b\u6700\u540e\u4e00\u4f4d\u7684\u6240\u6709\u5b50\u4e32 <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u540e\u7f00<\/strong>\uff1a\u4e0d\u5305\u542b\u7b2c\u4e00\u4f4d\u7684\u6240\u6709\u5b50\u4e32 <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6700\u957f\u76f8\u7b49\u524d\u540e\u7f00\u957f\u5ea6\uff08LPS\uff09<\/strong>\uff1a\u6700\u957f\u540c\u65f6\u76f8\u7b49\u7684\u524d\u7f00\u3001\u540e\u7f00\u957f\u5ea6\uff0c\u8bb0\u4e3a <code>next[i]<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">next\u6570\u7ec4\u5904\u7406\u903b\u8f91<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u521d\u59cb\u5316  j=0,next = new Array(m).fill(0)<\/li>\n\n\n\n<li>\u524d\u540e\u7f00\u4e0d\u76f8\u540c\u7684\u60c5\u51b5\uff0c\u4f7f\u7528while\uff0c\u800c\u4e0d\u662fif\uff0cwhile(j&gt;0 &amp;&amp; s[i]!=s[j]) j=next[j-1]<\/li>\n\n\n\n<li>\u524d\u540e\u7f00\u76f8\u540c\u7684\u60c5\u51b5\uff0cj++<\/li>\n\n\n\n<li>\u66f4\u65b0next\u6570\u7ec4  next[i] = j<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>function getNext(pattern) {\n    const m = pattern.length;\n    const next = new Array(m).fill(0);\n    let j = 0;\n    for (let i = 1; i &lt; m; i++) {\n        while (j &gt; 0 &amp;&amp; pattern&#91;i] !== pattern&#91;j]) {\n            j = next&#91;j - 1];\n        }\n        if (pattern&#91;i] === pattern&#91;j]) {\n            j++;\n            next&#91;i] = j;\n        }\n    }\n    return next;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>a a b a a f\nj\n&nbsp;i\n0 1 0 1 2 0\nnext&#91;i]\u7684\u503c\u662fj\u7684\u5f53\u524d\u4f4d\u7f6e++\n\n\u5982\u679c\u8f93\u5165\uff1aaabaaf\n&#91; 0, 1, 0, 1, 2, 0 ] next<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">KMP \u5339\u914d\u4e3b\u903b\u8f91<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>i\uff1a\u4e3b\u4e32 S \u6307\u9488\uff1bj\uff1a\u6a21\u5f0f\u4e32 T \u6307\u9488<\/li>\n\n\n\n<li>S[i] == T[j]\uff1ai++, j++<\/li>\n\n\n\n<li>j \u8d70\u5230\u6a21\u5f0f\u4e32\u672b\u5c3e\uff1a\u5339\u914d\u6210\u529f\uff0c\u8fd4\u56de\u8d77\u59cb\u4e0b\u6807 <code>i - j<\/code><\/li>\n\n\n\n<li>\u5931\u914d\u4e14 j\u22600\uff1a<code>j = next[j-1]<\/code><\/li>\n\n\n\n<li>\u5931\u914d j=0\uff1ai++<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>function KMP(s, t) {\n    const n = s.length, m = t.length;\n    if (m === 0) return 0;\n    const next = getNext(t);\n    let i = 0, j = 0;\n    while (i &lt; n) {\n        if (s&#91;i] === t&#91;j]) {\n            i++, j++;\n        }\n        if (j === m) return i - j;\n        if (i &lt; n &amp;&amp; s&#91;i] !== t&#91;j]) {\n            if (j !== 0) {\n                j = next&#91;j - 1];\n            } else {\n                i++;\n            }\n        }\n    }\n    return -1;\n}\n\nconsole.log(KMP(sInput, tInput));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u51fa\u4e24\u4e2a\u5b57\u7b26\u4e32 S\uff08\u4e3b\u4e32\uff09\u3001T\u6a21\u5f0f\u4e32\uff09\uff0c\u8f93\u51fa T ..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,11,23],"tags":[],"class_list":["post-574","post","type-post","status-publish","format-standard","hentry","category-js","category-front","category-23"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/js\/\" rel=\"category tag\">JS<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/\" rel=\"category tag\">\u524d\u7aef<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/%e8%af%95%e9%a2%98\/\" rel=\"category tag\">\u8bd5\u9898<\/a>","tag_info":"\u8bd5\u9898","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=574"}],"version-history":[{"count":4,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/574\/revisions"}],"predecessor-version":[{"id":580,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/574\/revisions\/580"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}