{"id":184,"date":"2019-11-14T03:34:00","date_gmt":"2019-11-14T03:34:00","guid":{"rendered":"https:\/\/imwarming.com\/?p=184"},"modified":"2019-11-14T03:34:00","modified_gmt":"2019-11-14T03:34:00","slug":"kmp","status":"publish","type":"post","link":"https:\/\/imwarming.com\/?p=184","title":{"rendered":"kmp"},"content":{"rendered":"<p>kmp\uff0cnext\u5e26\u4f18\u5316<\/p>\n<div class=\"cnblogs_code\">\n<pre>#include&lt;iostream&gt;<span style=\"color: #000000;\">\n#include<\/span>&lt;bits\/stdc++.h&gt;\n<span style=\"color: #0000ff;\">using<\/span> <span style=\"color: #0000ff;\">namespace<\/span><span style=\"color: #000000;\"> std;\n<\/span><span style=\"color: #0000ff;\">void<\/span> getnext(<span style=\"color: #0000ff;\">char<\/span> p[],<span style=\"color: #0000ff;\">int<\/span><span style=\"color: #000000;\"> next[]){\n    next[<\/span><span style=\"color: #800080;\">0<\/span>]=-<span style=\"color: #800080;\">1<\/span><span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000ff;\">int<\/span> i=<span style=\"color: #800080;\">0<\/span>,j=-<span style=\"color: #800080;\">1<\/span><span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000ff;\">int<\/span> n=<span style=\"color: #000000;\">strlen(p);\n    <\/span><span style=\"color: #0000ff;\">while<\/span>(i&lt;n-<span style=\"color: #800080;\">1<\/span><span style=\"color: #000000;\">){\n        <\/span><span style=\"color: #0000ff;\">if<\/span>(j==-<span style=\"color: #800080;\">1<\/span>||p[i]==<span style=\"color: #000000;\">p[j]){\n            i<\/span>++<span style=\"color: #000000;\">;\n            j<\/span>++<span style=\"color: #000000;\">;\n            <\/span><span style=\"color: #0000ff;\">if<\/span>(p[i]!=<span style=\"color: #000000;\">p[j])\n                next[i]<\/span>=<span style=\"color: #000000;\">j;\n            <\/span><span style=\"color: #0000ff;\">else<\/span><span style=\"color: #000000;\">\n                next[i]<\/span>=<span style=\"color: #000000;\">next[j];\n        }\n        <\/span><span style=\"color: #0000ff;\">else<\/span><span style=\"color: #000000;\">\n            j<\/span>=<span style=\"color: #000000;\">next[j];\n    }\n}\n<\/span><span style=\"color: #0000ff;\">int<\/span> kmp(<span style=\"color: #0000ff;\">char<\/span> s[],<span style=\"color: #0000ff;\">char<\/span> p[],<span style=\"color: #0000ff;\">int<\/span><span style=\"color: #000000;\"> next[]){\n    <\/span><span style=\"color: #0000ff;\">int<\/span> i=<span style=\"color: #800080;\">0<\/span>,j=<span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000ff;\">int<\/span> slen=<span style=\"color: #000000;\">strlen(s);\n    <\/span><span style=\"color: #0000ff;\">int<\/span> plen=<span style=\"color: #000000;\">strlen(p);\n    getnext(p,next);\n    <\/span><span style=\"color: #0000ff;\">while<\/span>(i&lt;slen&amp;&amp;j&lt;<span style=\"color: #000000;\">plen){\n        <\/span><span style=\"color: #0000ff;\">if<\/span>(j==-<span style=\"color: #800080;\">1<\/span>||s[i]==<span style=\"color: #000000;\">p[j]){\n            i<\/span>++<span style=\"color: #000000;\">;\n            j<\/span>++<span style=\"color: #000000;\">;\n        }\n        <\/span><span style=\"color: #0000ff;\">else<\/span><span style=\"color: #000000;\">\n            j<\/span>=<span style=\"color: #000000;\">next[j];\n    }\n    <\/span><span style=\"color: #0000ff;\">if<\/span>(j==<span style=\"color: #000000;\">plen){\n        <\/span><span style=\"color: #0000ff;\">return<\/span> i-j+<span style=\"color: #800080;\">1<\/span><span style=\"color: #000000;\">;\n    }\n    <\/span><span style=\"color: #0000ff;\">else<\/span> <span style=\"color: #0000ff;\">return<\/span> -<span style=\"color: #800080;\">1<\/span><span style=\"color: #000000;\">;\n}\n<\/span><span style=\"color: #0000ff;\">int<\/span><span style=\"color: #000000;\"> main(){\n    <\/span><span style=\"color: #0000ff;\">char<\/span> s[]=<span style=\"color: #800000;\">\"<\/span><span style=\"color: #800000;\">ababxbababcadfdsss<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000ff;\">char<\/span> p[]=<span style=\"color: #800000;\">\"<\/span><span style=\"color: #800000;\">ba<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000ff;\">int<\/span> next[<span style=\"color: #800080;\">20<\/span>]={<span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">};\n    cout<\/span>&lt;&lt;kmp(s,p,next)&lt;&lt;<span style=\"color: #000000;\">endl;\n    <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u8f93\u51fanext<\/span>\n    <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">int<\/span> i = <span style=\"color: #800080;\">0<\/span>; i &lt; strlen(p); ++<span style=\"color: #000000;\">i){\n        printf(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #800000;\">%d <\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #000000;\">,next[i]);\n    }\n}<\/span><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>kmp\uff0cnext\u5e26\u4f18\u5316 #include&lt;iostream&gt; #include&lt;b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-184","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v18.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>kmp - imwarming<\/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:\/\/imwarming.com\/?p=184\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"kmp - imwarming\" \/>\n<meta property=\"og:description\" content=\"kmp\uff0cnext\u5e26\u4f18\u5316 #include&lt;iostream&gt; #include&lt;b [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/imwarming.com\/?p=184\" \/>\n<meta property=\"og:site_name\" content=\"imwarming\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-14T03:34:00+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"warming\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/imwarming.com\/#website\",\"url\":\"https:\/\/imwarming.com\/\",\"name\":\"imwarming\",\"description\":\"\u6c38\u8fdc\u5e74\u8f7b\uff0c\u6c38\u8fdc\u70ed\u6cea\u76c8\u7736\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/imwarming.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/imwarming.com\/?p=184#webpage\",\"url\":\"https:\/\/imwarming.com\/?p=184\",\"name\":\"kmp - imwarming\",\"isPartOf\":{\"@id\":\"https:\/\/imwarming.com\/#website\"},\"datePublished\":\"2019-11-14T03:34:00+00:00\",\"dateModified\":\"2019-11-14T03:34:00+00:00\",\"author\":{\"@id\":\"https:\/\/imwarming.com\/#\/schema\/person\/9d76869a558bac6dd0d6d58f420ee8ea\"},\"breadcrumb\":{\"@id\":\"https:\/\/imwarming.com\/?p=184#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/imwarming.com\/?p=184\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/imwarming.com\/?p=184#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/imwarming.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"kmp\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/imwarming.com\/#\/schema\/person\/9d76869a558bac6dd0d6d58f420ee8ea\",\"name\":\"warming\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/imwarming.com\/#personlogo\",\"inLanguage\":\"zh-Hans\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c4a913eed88f7601b76bbf2b103472621195b6fa2f742af89b5ea185b60e7cff?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c4a913eed88f7601b76bbf2b103472621195b6fa2f742af89b5ea185b60e7cff?s=96&d=mm&r=g\",\"caption\":\"warming\"},\"sameAs\":[\"https:\/\/imwarming.com\"],\"url\":\"https:\/\/imwarming.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"kmp - imwarming","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:\/\/imwarming.com\/?p=184","og_locale":"zh_CN","og_type":"article","og_title":"kmp - imwarming","og_description":"kmp\uff0cnext\u5e26\u4f18\u5316 #include&lt;iostream&gt; #include&lt;b [&hellip;]","og_url":"https:\/\/imwarming.com\/?p=184","og_site_name":"imwarming","article_published_time":"2019-11-14T03:34:00+00:00","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"warming","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/imwarming.com\/#website","url":"https:\/\/imwarming.com\/","name":"imwarming","description":"\u6c38\u8fdc\u5e74\u8f7b\uff0c\u6c38\u8fdc\u70ed\u6cea\u76c8\u7736","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/imwarming.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"WebPage","@id":"https:\/\/imwarming.com\/?p=184#webpage","url":"https:\/\/imwarming.com\/?p=184","name":"kmp - imwarming","isPartOf":{"@id":"https:\/\/imwarming.com\/#website"},"datePublished":"2019-11-14T03:34:00+00:00","dateModified":"2019-11-14T03:34:00+00:00","author":{"@id":"https:\/\/imwarming.com\/#\/schema\/person\/9d76869a558bac6dd0d6d58f420ee8ea"},"breadcrumb":{"@id":"https:\/\/imwarming.com\/?p=184#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/imwarming.com\/?p=184"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/imwarming.com\/?p=184#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/imwarming.com\/"},{"@type":"ListItem","position":2,"name":"kmp"}]},{"@type":"Person","@id":"https:\/\/imwarming.com\/#\/schema\/person\/9d76869a558bac6dd0d6d58f420ee8ea","name":"warming","image":{"@type":"ImageObject","@id":"https:\/\/imwarming.com\/#personlogo","inLanguage":"zh-Hans","url":"https:\/\/secure.gravatar.com\/avatar\/c4a913eed88f7601b76bbf2b103472621195b6fa2f742af89b5ea185b60e7cff?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c4a913eed88f7601b76bbf2b103472621195b6fa2f742af89b5ea185b60e7cff?s=96&d=mm&r=g","caption":"warming"},"sameAs":["https:\/\/imwarming.com"],"url":"https:\/\/imwarming.com\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/imwarming.com\/index.php?rest_route=\/wp\/v2\/posts\/184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/imwarming.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imwarming.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/imwarming.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/imwarming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=184"}],"version-history":[{"count":0,"href":"https:\/\/imwarming.com\/index.php?rest_route=\/wp\/v2\/posts\/184\/revisions"}],"wp:attachment":[{"href":"https:\/\/imwarming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imwarming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imwarming.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}