{"id":65,"date":"2020-03-23T20:48:37","date_gmt":"2020-03-23T12:48:37","guid":{"rendered":"http:\/\/47.103.223.234:100\/?p=65"},"modified":"2021-05-01T15:40:33","modified_gmt":"2021-05-01T07:40:33","slug":"kmp","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/03\/23\/kmp\/","title":{"rendered":"kmp\u7b97\u6cd5\u539f\u7406"},"content":{"rendered":"<p>kmp\u662f\u4e00\u79cd\u5feb\u901f\u5339\u914d\u5b57\u7b26\u4e32\u7684\u7b97\u6cd5\u3002\u5b83\u7684\u6838\u5fc3\u662fNext[]\u6570\u7ec4\u3002<br \/>\n\u6a21\u677f\uff1a\u6bcd\u4e32t\uff0c\u5b50\u4e32s\uff0c\u6c42s\u5728t\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u4ee5\u53ca\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002<br \/>\n\u5728\u6734\u7d20\u7b97\u6cd5\u4e2d\uff0c\u6bcf\u4e00\u6b21\u5339\u914d\u5230\u4e00\u4e2a\u5b50\u4e32\u6216\u5931\u914d\u65f6\uff0c\u90fd\u4f1a\u8fd4\u56de\u5230\u5f00\u59cb\u5339\u914d\u7684\u5b57\u7b26\u7684\u4e0b\u4e00\u4f4d\u5728\u91cd\u65b0\u5339\u914d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6O(|st|)\uff0c\u660e\u663e\u592a\u6162\u4e86\u3002\u800ckmp\u7b97\u6cd5\u53ef\u4ee5\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u7f29\u5c0f\u4e3aO(s+t)\u3002<br \/>\nNext[]\u6570\u7ec4\u7684\u957f\u5ea6\u4e0e\u5b50\u4e32s\u76f8\u540c\uff0c\u5176\u4e2d\uff0cNext[i]\u8868\u793a<strong>s\u7684\u524di\u4e2a\u5b57\u7b26\u91cc\uff0c\u524d\u7f00\u4e0e\u540e\u7f00\u76f8\u540c\u7684\u6700\u5927\u957f\u5ea6(\u524d\u540e\u7f00\u4e0d\u80fd\u5305\u542b\u5168\u90e8\u5b57\u7b26)<\/strong>\uff0c\u4f8b\u5982<br \/>\n&quot;abcabc&quot;\uff0c\u8fd9\u4e2a\u5b50\u4e32\u4e2d\uff0cNext[]\u6570\u7ec4\u7684\u503c\u4f9d\u6b21\u4e3a:0,0,0,1,2,3.<br \/>\n\u8fd9\u4e2aNext[]\u6570\u7ec4\u529f\u80fd\u5f88\u5f3a\u5927\uff0c\u52a1\u5fc5\u8981\u5f7b\u5e95\u7406\u89e3\uff0c\u5e76\u80fd\u7528\u4e8e\u4efb\u4f55\u9700\u8981\u7684\u5730\u65b9\u3002<br \/>\nNext[]\u6570\u7ec4\u6c42\u53d6<\/p>\n<pre><code class=\"language-c++\">void GetNext(char *s,int len,int Next[]){\n    for (int i=2,j=0;i&lt;=len;i++){\n        while (j &amp;&amp; s[i]!=s[j+1])   j=Next[j];\n        if (s[i]==s[j+1])   j++;\n        Next[i] = j;\n    }\n    return;\n}<\/code><\/pre>\n<p>\u4e0b\u9762\u6765\u89e3\u91ca\u6c42\u53d6\u7684\u8fc7\u7a0b\u3002<br \/>\n\u5728\u6bcf\u4e00\u6b21\u5faa\u73af\u5f00\u59cb\u4e4b\u524d\uff0ci=i\uff0c\u8868\u793a\u8981\u6c42\u7684Next\u7684\u4e0b\u6807\uff0cj=Next[i-1],\u5373\u524di-1\u4e2a\u5b57\u7b26\u7684\u6700\u4f18\u89e3\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u4e4b\u95f4\u6709\u4ec0\u4e48\u5173\u7cfb\u5462\uff1f\u5f88\u660e\u663e\uff0cNext[i]\u7684\u503c\u6700\u5927\u4e3aNext[i-1]+1,\u5728s[i]==s[j+1]\u7684\u60c5\u51b5\u4e0b\u3002\u90a3\u4e48\u5f53\u4e8c\u8005\u4e0d\u7b49\u65f6\uff0c\u5373\u5931\u914d\u4e86\uff0cj\u8981\u8d4b\u503c\u4e3aNext[j]\uff0c\u7ee7\u7eed\u5224\u65ad\uff0c\u76f4\u81f3j\u4e3a0.\u8fd9\u6837\u7684\u8bdd\u8fd9\u4e2a\u51fd\u6570\u5c31\u5f88\u5bb9\u6613\u7406\u89e3\u4e86\u3002\u8fd9\u79cd\u6c42\u6cd5\u53ef\u4ee5\u4fdd\u8bc1\u89e3\u4e3a\u6700\u4f18\u3002<\/p>\n<p>\u6709\u4e86Next\u6570\u7ec4\uff0c\u90a3\u4e48\u8fd9\u4e2a\u95ee\u9898\u5c31\u5f88\u5bb9\u6613\u6c42\u89e3\u4e86\uff0c\u76f4\u63a5\u904d\u5386\u4e00\u6b21\u6bcd\u4e32\u5373\u53ef\u3002<br \/>\ni\u8868\u793a\u5f53\u524d\u626b\u63cf\u6bcd\u4e32t\u7684\u4e0b\u6807\uff0cj\u8868\u793a\u5f53\u524d\u626b\u63cf\u5b50\u4e32s\u7684\u4e0b\u6807\u3002\u5f53\u5339\u914d\u65f6\uff0cj++,i++;\u5f53\u5931\u914d\u65f6\uff0cj=Next[j]\uff0c\u7ee7\u7eed\u5339\u914d\u3002<\/p>\n<pre><code class=\"language-c++\">for (int i=1,j=0;i&lt;=len1;i++){\n    while (j &amp;&amp; t[i]!=s[j+1])   j=Next[j];\n    if (t[i]==s[j+1])   j++;\n    if (j==len2){\n        opera();     \/\/\u627e\u5230\u4e00\u6b21\u51fa\u73b0\u6b21\u6570\u540e\u7684\u76f8\u5173\u64cd\u4f5c\u3002\n        j=Next[j];\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>kmp\u662f\u4e00\u79cd\u5feb\u901f\u5339\u914d\u5b57\u7b26\u4e32\u7684\u7b97\u6cd5\u3002\u5b83\u7684\u6838\u5fc3\u662fNext[]\u6570\u7ec4\u3002 \u6a21\u677f\uff1a\u6bcd\u4e32t\uff0c\u5b50\u4e32s\uff0c\u6c42s\u5728t\u4e2d\u51fa\u73b0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/65"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":4,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"predecessor-version":[{"id":1136,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/65\/revisions\/1136"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}