{"id":833,"date":"2020-03-22T12:01:22","date_gmt":"2020-03-22T19:01:22","guid":{"rendered":"http:\/\/209.126.2.187\/?p=833"},"modified":"2020-04-12T19:31:47","modified_gmt":"2020-04-13T02:31:47","slug":"kmp","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2020\/03\/22\/kmp\/","title":{"rendered":"KMP"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this post, I will introduce some problems related to KMP. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">OJ<\/h2>\n\n\n\n\n<table id=\"tablepress-1\" class=\"tablepress tablepress-id-1\">\n<thead>\n<tr class=\"row-1\">\n\t<td class=\"column-1\"><\/td><th class=\"column-2\">Trick<\/th><th class=\"column-3\">Difficulty<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-striping row-hover\">\n<tr class=\"row-2\">\n\t<td class=\"column-1\"><a href=\"https:\/\/leetcode.com\/problems\/longest-happy-prefix\/\" rel=\"noopener noreferrer\" target=\"_blank\">LC 1392<\/a><\/td><td class=\"column-2\">Naive Pi<\/td><td class=\"column-3\">5-6 points<\/td>\n<\/tr>\n<tr class=\"row-3\">\n\t<td class=\"column-1\"><a href=\"https:\/\/leetcode.com\/problems\/rotate-string\/\" rel=\"noopener noreferrer\" target=\"_blank\">LC 796<\/a><\/td><td class=\"column-2\">Pi on B + \"#\" + A<\/td><td class=\"column-3\">3-4 points<\/td>\n<\/tr>\n<tr class=\"row-4\">\n\t<td class=\"column-1\"><a href=\"https:\/\/leetcode.com\/problems\/shortest-palindrome\/\" rel=\"noopener noreferrer\" target=\"_blank\">LC 214<\/a><\/td><td class=\"column-2\">Pi on A + \"#\" + A[::-1]<\/td><td class=\"column-3\">5-6 points<\/td>\n<\/tr>\n<tr class=\"row-5\">\n\t<td class=\"column-1\"><a href=\"https:\/\/leetcode.com\/problems\/implement-strstr\/\" rel=\"noopener noreferrer\" target=\"_blank\">LC 28<\/a><\/td><td class=\"column-2\">Pi on B + \"#\" + A<\/td><td class=\"column-3\">3-4 points<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!-- #tablepress-1 from cache -->\n\n\n\n<h2 class=\"wp-block-heading\">Details<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Pi <\/h3>\n\n\n\n<p> <\/p>\n\n\n\n<p><p class=\"ql-center-displayed-equation\" style=\"line-height: 26px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-05fbf39b405aa2b4ceb7ecff9bce26c6_l3.png\" height=\"26\" width=\"368\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#091;&#92;&#112;&#105;&#091;&#105;&#093;&#32;&#61;&#32;&#92;&#109;&#97;&#120;&#95;&#123;&#107;&#32;&#61;&#32;&#48;&#32;&#92;&#100;&#111;&#116;&#115;&#32;&#105;&#125;&#123;&#107;&#58;&#32;&#115;&#091;&#48;&#32;&#92;&#100;&#111;&#116;&#115;&#32;&#107;&#32;&#45;&#32;&#49;&#093;&#32;&#61;&#32;&#115;&#091;&#105;&#32;&#45;&#32;&#40;&#107;&#32;&#45;&#32;&#49;&#41;&#32;&#92;&#100;&#111;&#116;&#115;&#32;&#105;&#093;&#125;&#92;&#093;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p><\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-f7e1df85381b5eb796dffe9c60ff0de1_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#112;&#105;&#091;&#105;&#093;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"25\" style=\"vertical-align: -5px;\"\/> is the length of the longest prefix of substring <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-2da85424cbc2511de3fe83a54e60d4cd_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#115;&#091;&#48;&#92;&#100;&#111;&#116;&#115;&#32;&#105;&#093;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"58\" style=\"vertical-align: -5px;\"\/>, such that this prefix is also the suffix of <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-2da85424cbc2511de3fe83a54e60d4cd_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#115;&#091;&#48;&#92;&#100;&#111;&#116;&#115;&#32;&#105;&#093;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"58\" style=\"vertical-align: -5px;\"\/>. Note that this prefix excludes <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-2da85424cbc2511de3fe83a54e60d4cd_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#115;&#091;&#48;&#92;&#100;&#111;&#116;&#115;&#32;&#105;&#093;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"58\" style=\"vertical-align: -5px;\"\/> itself. By the definition, we have <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-dd0272bef71c1db0137643338193d565_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#112;&#105;&#091;&#48;&#093;&#32;&#61;&#32;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"62\" style=\"vertical-align: -5px;\"\/>. For example, we have,<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-070c51eef692bb515900ca9b8adb815b_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#112;&#105;&#40;&#34;&#97;&#98;&#99;&#97;&#98;&#99;&#100;&#34;&#41;&#32;&#61;&#32;&#091;&#48;&#44;&#32;&#48;&#44;&#32;&#48;&#44;&#32;&#49;&#44;&#32;&#50;&#44;&#32;&#51;&#44;&#32;&#48;&#093;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"247\" style=\"vertical-align: -5px;\"\/><\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-4b5023fa9fcb1f7f052c0b842ac2a76d_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#112;&#105;&#40;&#34;&#97;&#97;&#98;&#97;&#97;&#97;&#98;&#34;&#41;&#32;&#61;&#32;&#091;&#48;&#44;&#32;&#49;&#44;&#32;&#48;&#44;&#32;&#49;&#44;&#32;&#50;&#44;&#32;&#50;&#44;&#32;&#51;&#093;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"251\" style=\"vertical-align: -5px;\"\/><\/p>\n\n\n\n<p>Please refer to <a href=\"https:\/\/oi-wiki.org\/string\/kmp\/\">this wiki<\/a> if you want to know how the following codes work. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">vector&lt;int> get_pi(string const&amp;s) {\n    int n = s.size();\n    vector&lt;int> pi(n);\n    for (int i = 1; i &lt; n; ++i) {\n        int j = pi[i - 1];\n        while (j > 0 &amp;&amp; s[i] != s[j]) {\n            j = pi[j - 1];\n        }\n        pi[i] = j + (s[i] == s[j]);\n    }\n    return pi;\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce some problems related to KMP. OJ Details Pi &nbsp; &nbsp; is the length of the longest prefix of substring , such that this prefix is also the suffix of . Note that this prefix excludes itself. By the definition, we have . For example, we have, Please refer&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,54],"tags":[],"class_list":["post-833","post","type-post","status-publish","format-standard","hentry","category-alg","category-string"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=833"}],"version-history":[{"count":10,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/833\/revisions"}],"predecessor-version":[{"id":997,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/833\/revisions\/997"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}