{"id":26,"date":"2025-11-09T20:41:27","date_gmt":"2025-11-09T12:41:27","guid":{"rendered":"https:\/\/tblog.zeabur.app\/?p=26"},"modified":"2025-11-09T20:54:23","modified_gmt":"2025-11-09T12:54:23","slug":"%e9%a2%98%e8%a7%a3-cf877b-nikita-and-string","status":"publish","type":"post","link":"https:\/\/tblog.zeabur.app\/?p=26","title":{"rendered":"\u9898\u89e3 CF877B Nikita and string"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u9898\u76ee\u5927\u610f<\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u53ea\u5305\u542b\u5b57\u7b26 <code>a<\/code> \u548c <code>b<\/code> \u7684\u5b57\u7b26\u4e32\uff0c\u901a\u8fc7\u5220\u9664\u5b57\u7b26\uff08\u53ef\u4ee5\u4e0d\u5220\u9664\uff09\u5c06\u5176\u8f6c\u5316\u4e3a\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u7684\u6700\u957f\u5b57\u7b26\u4e32\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5b57\u7b26\u4e32\u53ef\u4ee5\u5206\u4e3a\u4e09\u6bb5\u3002<\/li>\n\n\n\n<li>\u7b2c\u4e00\u6bb5\u548c\u7b2c\u4e09\u6bb5\u4ec5\u5305\u542b <code>a<\/code>\u3002<\/li>\n\n\n\n<li>\u7b2c\u4e8c\u6bb5\u4ec5\u5305\u542b <code>b<\/code>\u3002<\/li>\n\n\n\n<li>\u6bcf\u4e00\u6bb5\u90fd\u53ef\u4ee5\u4e3a\u7a7a\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u51b3\u601d\u8def<\/h2>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\uff0c$dp_{i,j}$ \u8868\u793a\u524d $i$ \u4e2a\u5b57\u7b26\uff0c\u7b2c $j$ \u6bb5\u65f6\uff0c\u6700\u957f\u7f8e\u4e3d\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5176\u4e2d $j\\in{0,1,2}$\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u8003\u8651\u9010\u4e2a\u8003\u8651\u5b57\u7b26\u4e32 $s$ \u7684\u6bcf\u4e00\u4f4d\uff1a<\/p>\n\n\n\n<p>\u5982\u679c $s_i$ \u662f <code>a<\/code>\uff0c\u90a3\u4e48\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>$dp[i][0]=dp[i-1][0]+1$\uff0c\u5373\u7b2c\u4e00\u6bb5\u5ef6\u957f\uff1b<\/li>\n\n\n\n<li>$dp[i][1]=dp[i-1][1]$\uff0c\u5373\u7b2c\u4e8c\u6bb5\u4e0d\u53d8\uff1b<\/li>\n\n\n\n<li>$dp[i][2]=\\max(dp[i-1][1],dp[i-1][2])+1$\uff0c\u5373\u7b2c\u4e09\u6bb5\u5f00\u59cb\u6216\u5ef6\u957f\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c $s_i$ \u662f <code>b<\/code>\uff0c\u90a3\u4e48\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>$dp[i][0]=dp[i-1][0]$\uff0c\u5373\u7b2c\u4e00\u6bb5\u4e0d\u53d8\uff1b<\/li>\n\n\n\n<li>$dp[i][1]=\\max(dp[i-1][0],dp[i-1][1])+1$\uff0c\u5373\u7b2c\u4e8c\u6bb5\u5f00\u59cb\u6216\u5ef6\u957f\uff1b<\/li>\n\n\n\n<li>$dp[i][2]=dp[i-1][2]$\uff0c\u5373\u7b2c\u4e09\u6bb5\u4e0d\u53d8\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5219\u6700\u7ec8\u7b54\u6848\u662f $\\max(dp[n][0],\\max(dp[n][1],dp[n][2]))$\uff0c\u5176\u4e2d\uff0c$n$ \u4e3a\u5b57\u7b26\u4e32 $s$ \u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ee3\u7801<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;bits\/stdc++.h&gt;\n#include&lt;ext\/pb_ds\/assoc_container.hpp&gt;\n#include&lt;ext\/pb_ds\/tree_policy.hpp&gt;\n#define int long long\n#define tem template\n#define sizet size_t\n#define il inline\n#define uni union\n#define endl \"\\n\"\nconst int INF=0xfffffff;\nusing namespace std;\nusing namespace __gnu_pbds;\nusing namespace __gnu_cxx;\nint dp&#91;5005]&#91;3];\nsigned main()\n{\n    char a;\n    a=getchar();\n    dp&#91;0]&#91;0]=dp&#91;0]&#91;2]=(a=='a')?1:0;\/\/\u521d\u59cb\u5316\n    dp&#91;0]&#91;1]=1-dp&#91;0]&#91;0];\n    string s;\n    cin&gt;&gt;s;\n    for(int i=0;i&lt;s.size();i++)\n    {\n        if(s&#91;i]=='a')\/\/\u72b6\u6001\u8f6c\u79fb\n        {\n            dp&#91;i+1]&#91;0]=dp&#91;i]&#91;0]+1;\n            dp&#91;i+1]&#91;1]=dp&#91;i]&#91;1];\n            dp&#91;i+1]&#91;2]=max(dp&#91;i]&#91;1],dp&#91;i]&#91;2])+1;\n        }\n        else\n        {\n            dp&#91;i+1]&#91;0]=dp&#91;i]&#91;0];\n            dp&#91;i+1]&#91;1]=max(dp&#91;i]&#91;0],dp&#91;i]&#91;1])+1;\n            dp&#91;i+1]&#91;2]=dp&#91;i]&#91;2];\n        }\n    }\n    cout&lt;&lt;max(dp&#91;s.size()]&#91;0],max(dp&#91;s.size()]&#91;1],dp&#91;s.size()]&#91;2]))&lt;&lt;endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u65f6\u95f4\u590d\u6742\u5ea6<\/h3>\n\n\n\n<p>$O(|s|)$\uff0c\u5176\u4e2d $|s|$ \u662f\u5b57\u7b26\u4e32 $s$ \u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7a7a\u95f4\u590d\u6742\u5ea6<\/h3>\n\n\n\n<p>$O(|s|)$\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f \u7ed9\u5b9a\u4e00\u4e2a\u53ea\u5305\u542b\u5b57\u7b26 a \u548c b \u7684\u5b57\u7b26\u4e32\uff0c\u901a\u8fc7\u5220\u9664\u5b57\u7b26\uff08\u53ef\u4ee5\u4e0d\u5220\u9664\uff09\u5c06\u5176\u8f6c\u5316\u4e3a\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u7684\u6700\u957f\u5b57\u7b26 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[3],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-oi","tag-oi"],"featured_image_src":null,"author_info":{"display_name":"CaelumRadish_c","author_link":"https:\/\/tblog.zeabur.app\/?author=1"},"_links":{"self":[{"href":"https:\/\/tblog.zeabur.app\/index.php?rest_route=\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tblog.zeabur.app\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tblog.zeabur.app\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tblog.zeabur.app\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tblog.zeabur.app\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26"}],"version-history":[{"count":1,"href":"https:\/\/tblog.zeabur.app\/index.php?rest_route=\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":27,"href":"https:\/\/tblog.zeabur.app\/index.php?rest_route=\/wp\/v2\/posts\/26\/revisions\/27"}],"wp:attachment":[{"href":"https:\/\/tblog.zeabur.app\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tblog.zeabur.app\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tblog.zeabur.app\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}