{"id":1212,"date":"2024-10-13T09:00:00","date_gmt":"2024-10-13T01:00:00","guid":{"rendered":"https:\/\/seanxd.com\/?p=1212"},"modified":"2024-09-23T11:34:27","modified_gmt":"2024-09-23T03:34:27","slug":"zerojudge-a252","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/zerojudge-a252\/","title":{"rendered":"ZeroJudge A252: Another LCS"},"content":{"rendered":"\n<p>\u4e00\u822c LCS \u554f\u984c (Longest Common subsequence\uff5c\u6700\u9577\u5171\u540c\u5b50\u5b57\u4e32) \u5c31\u662f\u7d66\u5b9a\u5169\u500b\u5b57\u4e32\uff0c\u6c42\u51fa\u4ed6\u5011\u7684LCS\u3002<\/p>\n\n\n\n<p>\u70ba\u4e86\u7406\u89e3\u9019\u88e1\u5b50\u5b57\u4e32\u5b9a\u7fa9\uff0c\u8209\u4f8b\u4f86\u8aaa\uff0c\u5c0d\u65bc\u5b57\u4e32 abccdd\uff1aabc\u3001adda\u3001aca\u3001bda \u7b49\u90fd\u662f\u5b83\u7684\u5b50\u5b57\u4e32\uff0c\u4f46 adc\u3001bdde\u3001bddd \u7b49\u4e0d\u662f\u4ed6\u7684\u5b50\u5b57\u4e32\u3002<\/p>\n\n\n\n<p>\u5c0d\u65bc\u5169\u500b\u5b57\u4e32 accbbeffg\u3001fcebg\uff0c\u4ed6\u5011\u7684LCS\u9577\u5ea6\u70ba 3\uff0c\u800c LCS \u70ba cbg \u6216 ceg\u3002<\/p>\n\n\n\n<p>\u73fe\u5728\u6211\u5011\u628a\u554f\u984c\u5f04\u5f97\u96e3\u4e00\u9ede\uff0c\u7d66\u4e09\u500b\u5b57\u4e32\uff0c\u8acb\u6c42\u51fa\u4ed6\u5011\u7684 LCS \u9577\u5ea6\u70ba\u591a\u5c11\uff1f<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b\u6e2c\u8cc7<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7bc4\u4f8b\u8f38\u5165<\/th><th>\u7bc4\u4f8b\u8f38\u51fa<\/th><\/tr><\/thead><tbody><tr><td>\u6bcf\u500b\u6e2c\u8cc7\u6a94\u50c5\u5305\u542b\u4e00\u7b46\u6e2c\u8cc7\uff0c\u6bcf\u7b46\u6e2c\u8cc7\u6709\u4e09\u500b\u5b57\u4e32\u3002\u6e2c\u8cc7\u4fdd\u8b49\u4e09\u500b\u5b57\u4e32\u7684\u9577\u5ea6\u90fd\u4e0d\u8d85\u904e 100\uff0c\u800c\u4e14\u5b57\u4e32\u7686\u7531\u5c0f\u5beb\u5b57\u6bcd\u7d44\u6210\u3002<\/td><td>\u5c0d\u6bcf\u7b46\u6e2c\u8cc7\uff0c\u8f38\u51fa LCS \u7684\u9577\u5ea6\u3002<\/td><\/tr><tr><td>abe<br>acb<br>babcd<\/td><td>2<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">ZeroJudge A252 \u7bc4\u4f8b\u6e2c\u8cc7<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u984c\u601d\u8def<\/h2>\n\n\n\n<p>\u5047\u8a2d\u4e09\u500b\u5b57\u4e32\u5206\u5225\u70ba a\u3001b\u3001c\uff0c\u5ba3\u544a\u4e00\u500b\u4e09\u7dad\u9663\u5217\uff0c\u4e26\u4e14\u8dd1\u4e00\u500b a.length()*b.length()*c.length() \u7684 For \u8ff4\u5708\uff0c\u5982\u679c \u76ee\u524d\u8dd1\u5230\u7684\u4e09\u500b\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u8981\u5224\u65b7\u4f7f\u5426\u6709\u4efb\u4e00\u6578\u503c\u662f\u7b2c\u4e00\u500b\uff0c\u4e5f\u5c31\u662f 0\uff0c\u5982\u679c i\u3001j\u3001k \u6709\u4e00\u500b\u662f 0 \u7684\u8a71\uff0c\u5247\u5c07 \u4e09\u7dad\u9663\u5217[i][j][k] \u8a2d\u70ba 1\uff0c\u5426\u5247\u5c07 \u4e09\u7dad\u9663\u5217[i][j][k] \u8a2d\u70ba \u4e09\u7dad\u9663\u5217[i-1][j-1][k-1]\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u5169\u6578\u4e0d\u76f8\u7b49\uff0c\u5247\u4e5f\u8981\u5224\u65b7 i\u3001j\u3001k \u662f\u5426\u6709 0\uff0c\u5982\u679c\u90fd\u4e0d\u662f\u7684\u8a71\u5c31\u662f\u5c07 \u4e09\u7dad\u9663\u5217[i][j][k] \u8a2d\u70ba \u4e09\u7dad\u9663\u5217[i-1][j-1][k-1]\u3002\u9664\u6b64\u4e4b\u5916\u9084\u8981\u5224\u65b7\u53ea\u6709 i\u3001j\u3001k \u5176\u4e2d\u4e00\u500b\u6216\u5169\u500b\u7b49\u65bc 0 \u7684\u60c5\u6cc1\uff0c\u9019\u7a2e\u60c5\u6cc1\u5c31\u662f\u5c07\u6578\u503c\u8a2d\u70ba\u4e0d\u662f 0 \u7684\u90a3\u908a -1 \u4f4d\u7f6e\u3002\u5982\u679c\u90fd\u662f 0 \u7684\u8a71\u5c31\u5c07 \u4e09\u7dad\u9663\u5217[i][j][k] \u8a2d\u70ba 0\u3002<\/p>\n\n\n\n<p>\u6700\u5f8c\u7684\u7b54\u6848\u5c31\u662f \u4e09\u7dad\u9663\u5217[a.length()-1][b.length()-1][c.length()-1]\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=a252\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge A252: Another LCS<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    string a, b, c;\n    cin &gt;&gt; a &gt;&gt; b &gt;&gt; c;\n    int DP[100][100][100] = {};\n    for (int i = 0; i&lt;a.length(); i++) {\n        for (int j = 0; j&lt;b.length(); j++) {\n            for (int k = 0; k&lt;c.length(); k++) {\n                if (a[i] == b[j] && b[j] == c[k]) {\n                    if (i &gt; 0 && j &gt; 0 && k &gt; 0) {\n                        DP[i][j][k] = DP[i-1][j-1][k-1];\n                    }\n                    else {\n                        DP[i][j][k] = 0;\n                    }\n                    DP[i][j][k]++;\n                }\n                else {\n                    if (i &gt; 0 && j &gt; 0 && k &gt; 0) {\n                        DP[i][j][k] = max(max(DP[i-1][j][k], DP[i][j-1][k]), DP[i][j][k-1]);\n                    }\n                    else if (i &gt; 0 && j &gt; 0) {\n                        DP[i][j][k] = max(DP[i-1][j][k], DP[i][j-1][k]);\n                    }\n                    else if (j &gt; 0 && k &gt; 0) {\n                        DP[i][j][k] = max(DP[i][j-1][k], DP[i][j][k-1]);\n                    }\n                    else if (i &gt; 0 && k &gt; 0) {\n                        DP[i][j][k] = max(DP[i-1][j][k], DP[i][j][k-1]);\n                    }\n                    else if (i &gt; 0) {\n                        DP[i][j][k] = DP[i-1][j][k];\n                    }\n                    else if (j &gt; 0) {\n                        DP[i][j][k] = DP[i][j-1][k];\n                    }\n                    else if (k &gt; 0) {\n                        DP[i][j][k] = DP[i][j][k-1];\n                    }\n                    else DP[i][j][k] = 0;\n                }\n            }\n        }\n    }\n    cout &lt;&lt; DP[a.length()-1][b.length()-1][c.length()-1] &lt;&lt; &quot;\\n&quot;;\n}\n\n\/\/ZeroJudge A252\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u822c LCS \u554f\u984c (Longest Common subsequence\uff5c\u6700\u9577\u5171\u540c\u5b50\u5b57\u4e32) \u5c31\u662f\u7d66\u5b9a\u5169\u500b\u5b57\u4e32 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[29],"tags":[39,8,11,9],"class_list":["post-1212","post","type-post","status-publish","format-standard","hentry","category-zerojudge-","tag-39","tag-8","tag-11","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/comments?post=1212"}],"version-history":[{"count":2,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1212\/revisions"}],"predecessor-version":[{"id":1214,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1212\/revisions\/1214"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=1212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=1212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=1212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}