{"id":156,"date":"2025-01-12T12:57:13","date_gmt":"2025-01-12T04:57:13","guid":{"rendered":"http:\/\/150.230.63.147\/blog\/?p=156"},"modified":"2025-01-12T12:57:13","modified_gmt":"2025-01-12T04:57:13","slug":"d-prefixes-and-suffixes-%e8%a7%a3%e6%9e%90%e6%80%9d%e7%b6%ad%e3%80%81%e5%ad%97%e4%b8%b2%e3%80%81z-algo","status":"publish","type":"post","link":"https:\/\/blog.seekrealthing.net\/blog\/?p=156","title":{"rendered":"D. Prefixes and Suffixes \u89e3\u6790(\u601d\u7dad\u3001\u5b57\u4e32\u3001Z-Algo)"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><h1>Codeforce 432 D. Prefixes and Suffixes \u89e3\u6790(\u601d\u7dad\u3001\u5b57\u4e32\u3001Z-Algo)<\/h1>\n<p>\u4eca\u5929\u6211\u5011\u4f86\u770b\u770bCF432D\n<a href=\"https:\/\/codeforces.com\/problemset\/problem\/432\/D\">\u984c\u76ee\u9023\u7d50<\/a><\/p>\n<blockquote>\n<p><strong>\u984c\u76ee<\/strong>\n\u7565\uff0c\u8acb\u76f4\u63a5\u770b\u539f\u984c\u3002<\/p>\n<\/blockquote>\n<h3>\u524d\u8a00<\/h3>\n<p>\u5be6\u969b\u4e0a\u81ea\u5df1\u6839\u672c\u6c92\u5be6\u4f5c\u904eZ-Algo\uff0c\u611f\u89ba\u5f88\u6296\u3002<\/p>\n<h3>\u60f3\u6cd5<\/h3>\n<p>\u9996\u5148\u770b\u5230\u4e86\u5f8c\u7db4\u548c\u524d\u7db4\u7684\u95dc\u4fc2\uff0c\u5c31\u611f\u89ba\u548cKMP\u6216\u8005Z-Algo\u812b\u4e0d\u4e86\u95dc\u4fc2\u3002\u7a0d\u5fae\u8907\u7fd2\u4e00\u4e0bKMP\u548cZ-Algo\u7684DP\u8a08\u7b97\u7684\u610f\u7fa9\uff0c\u767c\u73fe\u4e86\u57fa\u672c\u4e0a\u5c31\u662fZ-Algo\u5b58\u7684\u8cc7\u6599\u7684\u61c9\u7528\u3002\n\u5c0d\u65bc\u7b2c\u4e00\u500b\u554f\u984c\uff1a\u6709\u5e7e\u500b\u5f8c\u7db4\u7b49\u65bc\u524d\u7db4\uff0c\u9019\u986f\u7136\u5c31\u662f\u8d64\u88f8\u88f8\u7684\u7528Z-Algo\u7684\u8cc7\u6599\u3002\n\u5c0d\u65bc\u7b2c\u4e8c\u500b\u554f\u984c\uff1a\u53ea\u8981\u89c0\u5bdf\u5230\uff0c\u5c0d\u65bc\u4e00\u500b\u9577\u5ea6\u70ba$l$\u7684\u524d\u7db4(\u7b49\u65bc\u5f8c\u7db4)\uff0c\u4efb\u4f55$Z[i]\\ge l$\u90fd\u6703\u5c0d\u9019\u500b\u5b50\u5b57\u4e32\u9020\u6210\u8ca2\u737b\uff0c\u4e5f\u56e0\u6b64\u53ea\u8981\u7dad\u8b77\u4e00\u500b$Z[i]$\u7684\u5f8c\u7db4\u548c\u5373\u53ef\u8f15\u9b06\u5f97\u5230\u7b54\u6848\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u4e00\u822c\u4f86\u8aaa$Z[0]=0$\uff0c\u56e0\u6b64\u6700\u5f8c\u6211\u5011\u9700\u8981\u81ea\u5df1\u6539\u6210$Z[0]=length$\u3002<\/p>\n<h3>\u7a0b\u5f0f\u78bc:<\/h3>\n<pre><code class=\"language-cpp=\">const int _n=1e5+10;\nint t,n,Z[_n],LEN,ans[_n],cnt,c[_n];\nstring s;\nmain(void) {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n  cin&gt;&gt;s;LEN=s.length();n=LEN;\n  int L = 0, R = 0;\n  for (int i = 1; i &lt; LEN; i++) {\n    if (i &gt; R) {\n      L = R = i;\n      while (R &lt; n &amp;&amp; s[R-L] == s[R]) R++;\n      Z[i] = R-L; R--;\n    } else {\n      int k = i-L;\n      if (Z[k] &lt; R-i+1) Z[i] = Z[k];\n      else {\n        L = i;\n        while (R &lt; n &amp;&amp; s[R-L] == s[R]) R++;\n        Z[i] = R-L; R--;\n      }\n    }\n  }\n  Z[0]=LEN;\n  rep(i,0,LEN)if(Z[i] and Z[i]&gt;=LEN-i)ans[Z[i]]++;\n  rep(i,1,LEN+1)if(ans[i])cnt++;\n  cout&lt;&lt;cnt&lt;&lt;'\\n';cnt=0;\n  rep(i,0,LEN)if(Z[i])c[Z[i]]++;\n  vector&lt;PII&gt; p;per(i,1,LEN+1){\n    cnt+=c[i];if(ans[i])p.pb({i,cnt});\n  }per(i,0,SZ(p))cout&lt;&lt;p[i].fi&lt;&lt;' '&lt;&lt;p[i].se&lt;&lt;'\\n';\n  return 0;\n}\n<\/code><\/pre>\n<p>\u6a19\u982d\u3001\u6a21\u677f\u8acb\u9edeSubmission\u770b\n<a href=\"https:\/\/codeforces.com\/contest\/432\/submission\/99213875\">Submission<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[3],"tags":[10,41,40,8],"class_list":["post-156","post","type-post","status-publish","format-standard","hentry","category-ojcode","tag-codeforce","tag-z-algo","tag-40","tag-8"],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>D. Prefixes and Suffixes \u89e3\u6790(\u601d\u7dad\u3001\u5b57\u4e32\u3001Z-Algo) - Logic Formatting<\/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:\/\/blog.seekrealthing.net\/blog\/?p=156\" \/>\n<meta property=\"og:locale\" content=\"zh_TW\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"D. Prefixes and Suffixes \u89e3\u6790(\u601d\u7dad\u3001\u5b57\u4e32\u3001Z-Algo) - Logic Formatting\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.seekrealthing.net\/blog\/?p=156\" \/>\n<meta property=\"og:site_name\" content=\"Logic Formatting\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-12T04:57:13+00:00\" \/>\n<meta name=\"author\" content=\"jeff\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005:\" \/>\n\t<meta name=\"twitter:data1\" content=\"jeff\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9810\u4f30\u95b1\u8b80\u6642\u9593\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\u9418\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=156#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=156\"},\"author\":{\"name\":\"jeff\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/person\/233e80a0fee3d4706f067f2e88c0b454\"},\"headline\":\"D. Prefixes and Suffixes \u89e3\u6790(\u601d\u7dad\u3001\u5b57\u4e32\u3001Z-Algo)\",\"datePublished\":\"2025-01-12T04:57:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=156\"},\"wordCount\":33,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#organization\"},\"keywords\":[\"codeforce\",\"Z-Algo\",\"\u5b57\u4e32\",\"\u601d\u7dad\"],\"articleSection\":[\"ojcode\"],\"inLanguage\":\"zh-TW\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blog.seekrealthing.net\/blog\/?p=156#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=156\",\"url\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=156\",\"name\":\"D. Prefixes and Suffixes \u89e3\u6790(\u601d\u7dad\u3001\u5b57\u4e32\u3001Z-Algo) - Logic Formatting\",\"isPartOf\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#website\"},\"datePublished\":\"2025-01-12T04:57:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=156#breadcrumb\"},\"inLanguage\":\"zh-TW\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.seekrealthing.net\/blog\/?p=156\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=156#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9801\",\"item\":\"https:\/\/blog.seekrealthing.net\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"D. Prefixes and Suffixes \u89e3\u6790(\u601d\u7dad\u3001\u5b57\u4e32\u3001Z-Algo)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#website\",\"url\":\"https:\/\/blog.seekrealthing.net\/blog\/\",\"name\":\"Logic Formatting\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.seekrealthing.net\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-TW\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#organization\",\"name\":\"Logic Formatting\",\"url\":\"https:\/\/blog.seekrealthing.net\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-TW\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.seekrealthing.net\/blog\/wp-content\/uploads\/2025\/02\/android-chrome-512x512-1.png\",\"contentUrl\":\"https:\/\/blog.seekrealthing.net\/blog\/wp-content\/uploads\/2025\/02\/android-chrome-512x512-1.png\",\"width\":512,\"height\":512,\"caption\":\"Logic Formatting\"},\"image\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/person\/233e80a0fee3d4706f067f2e88c0b454\",\"name\":\"jeff\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-TW\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f9b571b28c252728386f0002b35d9cb9a9028f740b0f9ca5ad678e7eb21c4857?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f9b571b28c252728386f0002b35d9cb9a9028f740b0f9ca5ad678e7eb21c4857?s=96&d=mm&r=g\",\"caption\":\"jeff\"},\"sameAs\":[\"http:\/\/150.230.63.147\/blog\"],\"url\":\"https:\/\/blog.seekrealthing.net\/blog\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"D. Prefixes and Suffixes \u89e3\u6790(\u601d\u7dad\u3001\u5b57\u4e32\u3001Z-Algo) - Logic Formatting","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:\/\/blog.seekrealthing.net\/blog\/?p=156","og_locale":"zh_TW","og_type":"article","og_title":"D. Prefixes and Suffixes \u89e3\u6790(\u601d\u7dad\u3001\u5b57\u4e32\u3001Z-Algo) - Logic Formatting","og_url":"https:\/\/blog.seekrealthing.net\/blog\/?p=156","og_site_name":"Logic Formatting","article_published_time":"2025-01-12T04:57:13+00:00","author":"jeff","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005:":"jeff","\u9810\u4f30\u95b1\u8b80\u6642\u9593":"1 \u5206\u9418"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.seekrealthing.net\/blog\/?p=156#article","isPartOf":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/?p=156"},"author":{"name":"jeff","@id":"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/person\/233e80a0fee3d4706f067f2e88c0b454"},"headline":"D. Prefixes and Suffixes \u89e3\u6790(\u601d\u7dad\u3001\u5b57\u4e32\u3001Z-Algo)","datePublished":"2025-01-12T04:57:13+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/?p=156"},"wordCount":33,"commentCount":0,"publisher":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/#organization"},"keywords":["codeforce","Z-Algo","\u5b57\u4e32","\u601d\u7dad"],"articleSection":["ojcode"],"inLanguage":"zh-TW","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.seekrealthing.net\/blog\/?p=156#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.seekrealthing.net\/blog\/?p=156","url":"https:\/\/blog.seekrealthing.net\/blog\/?p=156","name":"D. Prefixes and Suffixes \u89e3\u6790(\u601d\u7dad\u3001\u5b57\u4e32\u3001Z-Algo) - Logic Formatting","isPartOf":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/#website"},"datePublished":"2025-01-12T04:57:13+00:00","breadcrumb":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/?p=156#breadcrumb"},"inLanguage":"zh-TW","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.seekrealthing.net\/blog\/?p=156"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.seekrealthing.net\/blog\/?p=156#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9801","item":"https:\/\/blog.seekrealthing.net\/blog"},{"@type":"ListItem","position":2,"name":"D. Prefixes and Suffixes \u89e3\u6790(\u601d\u7dad\u3001\u5b57\u4e32\u3001Z-Algo)"}]},{"@type":"WebSite","@id":"https:\/\/blog.seekrealthing.net\/blog\/#website","url":"https:\/\/blog.seekrealthing.net\/blog\/","name":"Logic Formatting","description":"","publisher":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.seekrealthing.net\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-TW"},{"@type":"Organization","@id":"https:\/\/blog.seekrealthing.net\/blog\/#organization","name":"Logic Formatting","url":"https:\/\/blog.seekrealthing.net\/blog\/","logo":{"@type":"ImageObject","inLanguage":"zh-TW","@id":"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/blog.seekrealthing.net\/blog\/wp-content\/uploads\/2025\/02\/android-chrome-512x512-1.png","contentUrl":"https:\/\/blog.seekrealthing.net\/blog\/wp-content\/uploads\/2025\/02\/android-chrome-512x512-1.png","width":512,"height":512,"caption":"Logic Formatting"},"image":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/person\/233e80a0fee3d4706f067f2e88c0b454","name":"jeff","image":{"@type":"ImageObject","inLanguage":"zh-TW","@id":"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f9b571b28c252728386f0002b35d9cb9a9028f740b0f9ca5ad678e7eb21c4857?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f9b571b28c252728386f0002b35d9cb9a9028f740b0f9ca5ad678e7eb21c4857?s=96&d=mm&r=g","caption":"jeff"},"sameAs":["http:\/\/150.230.63.147\/blog"],"url":"https:\/\/blog.seekrealthing.net\/blog\/?author=1"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=156"}],"version-history":[{"count":1,"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/156\/revisions"}],"predecessor-version":[{"id":157,"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/156\/revisions\/157"}],"wp:attachment":[{"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}