{"id":1177,"date":"2022-05-28T20:31:18","date_gmt":"2022-05-28T12:31:18","guid":{"rendered":"https:\/\/sanlangcode.com\/?p=1177"},"modified":"2022-05-28T20:31:18","modified_gmt":"2022-05-28T12:31:18","slug":"hj65-%e6%9f%a5%e6%89%be%e4%b8%a4%e4%b8%aa%e5%ad%97%e7%ac%a6%e4%b8%b2ab%e4%b8%ad%e7%9a%84%e6%9c%80%e9%95%bf%e5%85%ac%e5%85%b1%e5%ad%90%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/sanlangcode.com\/index.php\/2022\/05\/28\/hj65-%e6%9f%a5%e6%89%be%e4%b8%a4%e4%b8%aa%e5%ad%97%e7%ac%a6%e4%b8%b2ab%e4%b8%ad%e7%9a%84%e6%9c%80%e9%95%bf%e5%85%ac%e5%85%b1%e5%ad%90%e4%b8%b2\/","title":{"rendered":"HJ65 \u67e5\u627e\u4e24\u4e2a\u5b57\u7b26\u4e32a,b\u4e2d\u7684\u6700\u957f\u516c\u5171\u5b50\u4e32"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u63cf\u8ff0<\/h2>\n\n\n\n<p>\u67e5\u627e\u4e24\u4e2a\u5b57\u7b26\u4e32a,b\u4e2d\u7684\u6700\u957f\u516c\u5171\u5b50\u4e32\u3002\u82e5\u6709\u591a\u4e2a\uff0c\u8f93\u51fa\u5728\u8f83\u77ed\u4e32\u4e2d\u6700\u5148\u51fa\u73b0\u7684\u90a3\u4e2a\u3002\u6ce8\uff1a\u5b50\u4e32\u7684\u5b9a\u4e49\uff1a\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5220\u53bb\u524d\u7f00\u548c\u540e\u7f00\uff08\u4e5f\u53ef\u4ee5\u4e0d\u5220\uff09\u5f62\u6210\u7684\u5b57\u7b26\u4e32\u3002\u8bf7\u548c\u201c\u5b50\u5e8f\u5217\u201d\u7684\u6982\u5ff5\u5206\u5f00\uff01<br>\u6570\u636e\u8303\u56f4\uff1a\u5b57\u7b26\u4e32\u957f\u5ea61\\le length \\le300 \\1\u2264<em>l<\/em><em>e<\/em><em>n<\/em><em>g<\/em><em>t<\/em><em>h<\/em>\u2264300&nbsp;\u8fdb\u9636\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(n^3)\\<em>O<\/em>(<em>n<\/em>3)&nbsp;\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n)\\<em>O<\/em>(<em>n<\/em>)&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f93\u5165\u63cf\u8ff0\uff1a<\/h3>\n\n\n\n<p>\u8f93\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f93\u51fa\u63cf\u8ff0\uff1a<\/h3>\n\n\n\n<p>\u8fd4\u56de\u91cd\u590d\u51fa\u73b0\u7684\u5b57\u7b26<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u793a\u4f8b1<\/h2>\n\n\n\n<p>\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">abcdefghijklmnop\nabcsafjklmnopqrstuvw<\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">jklmnop<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\npublic class Main{\n    public static void main(String&#91;]args){\n        Scanner sc=new Scanner(System.in);\n        while(sc.hasNext()){\n            String s1=sc.nextLine();\n            String s2=sc.nextLine();\n            System.out.println(longString(s1,s2));\n        }\n    }\n    \n    \/\/ \u52a8\u6001\u89c4\u5212\n    public static String longString(String str1, String str2) {\n        String temp = \"\";\n        \/\/ \u4fdd\u8bc1str1\u662f\u8f83\u77ed\u5b57\u7b26\u4e32\n        if (str1.length() > str2.length()) {\n            temp = str1;\n            str1 = str2;\n            str2 = temp;\n        }\n        int m = str1.length() + 1;\n        int n = str2.length() + 1;\n        \/\/ \u8868\u793a\u5728\u8f83\u77ed\u5b57\u7b26\u4e32str1\u4ee5\u7b2ci\u4e2a\u5b57\u7b26\u7ed3\u5c3e\uff0cstr2\u4e2d\u4ee5\u7b2cj\u4e2a\u5b57\u7b26\u7ed3\u5c3e\u65f6\u7684\u516c\u5171\u5b50\u4e32\u957f\u5ea6\u3002\n        int&#91;]&#91;] dp = new int&#91;m]&#91;n];\n        \/\/ \u5339\u914d\u5b57\u7b26\uff0c\u5e76\u8bb0\u5f55\u6700\u5927\u503c\u7684str1\u7684\u7ed3\u5c3e\u4e0b\u6807\n        int max = 0;\n        int index = 0;\n        \/\/ \u4ece\u5de6\u5411\u53f3\u9012\u63a8\uff0ci\u4e3a\u77ed\u5b57\u7b26\u4e32str1\u7684\u7ed3\u5c3e\u7d22\u5f15\uff0cj\u4e3astr2\u7684\u7ed3\u5c3e\u7d22\u5f15\n        for (int i=1; i &lt; m; i++) {\n            for (int j=1; j &lt; n; j++) {\n                if (str1.charAt(i-1) == str2.charAt(j-1)) {\n                    \/\/ \u76f8\u7b49\u5219\u8ba1\u6570\n                    dp&#91;i]&#91;j] = dp&#91;i-1]&#91;j-1] + 1;\n                    \/\/ \u4e0d\u65ad\u66f4\u65b0\u53d8\u91cf\n                    if (dp&#91;i]&#91;j] > max) {\n                        max = dp&#91;i]&#91;j];\n                        index = i;\n                    }\n                }\n            }\n        }\n        \/\/ \u622a\u53d6\u6700\u5927\u516c\u5171\u5b50\u4e32\n        return str1.substring(index-max, index);\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u63cf\u8ff0 \u67e5\u627e\u4e24\u4e2a\u5b57\u7b26\u4e32a,b\u4e2d\u7684\u6700&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1114,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1177","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-4"],"_links":{"self":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/1177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/comments?post=1177"}],"version-history":[{"count":0,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/1177\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media\/1114"}],"wp:attachment":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media?parent=1177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/categories?post=1177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/tags?post=1177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}