{"id":2865,"date":"2022-04-18T23:18:42","date_gmt":"2022-04-18T15:18:42","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2865"},"modified":"2022-04-18T23:18:42","modified_gmt":"2022-04-18T15:18:42","slug":"dp%e7%ac%ac19%e5%a4%a9","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2865","title":{"rendered":"dp\u7b2c19\u5929"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/is-subsequence\/\">392. \u5224\u65ad\u5b50\u5e8f\u5217<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><br><em>\/**<br><\/em><em> *<br><\/em><em> 392. \u5224\u65ad\u5b50\u5e8f\u5217<br><\/em><em> \u7ed9\u5b9a\u5b57\u7b26\u4e32 s \u548c t \uff0c\u5224\u65ad s \u662f\u5426\u4e3a t \u7684\u5b50\u5e8f\u5217\u3002<br><\/em><em><br><\/em><em> \u5b57\u7b26\u4e32\u7684\u4e00\u4e2a\u5b50\u5e8f\u5217\u662f\u539f\u59cb\u5b57\u7b26\u4e32\u5220\u9664\u4e00\u4e9b\uff08\u4e5f\u53ef\u4ee5\u4e0d\u5220\u9664\uff09\u5b57\u7b26\u800c\u4e0d\u6539\u53d8\u5269\u4f59\u5b57\u7b26\u76f8\u5bf9\u4f4d\u7f6e\u5f62\u6210\u7684\u65b0\u5b57\u7b26\u4e32\u3002\uff08\u4f8b\u5982\uff0c\"ace\"\u662f\"abcde\"\u7684\u4e00\u4e2a\u5b50\u5e8f\u5217\uff0c\u800c\"aec\"\u4e0d\u662f\uff09\u3002<br><\/em><em><br><\/em><em> \u8fdb\u9636\uff1a<br><\/em><em><br><\/em><em> \u5982\u679c\u6709\u5927\u91cf\u8f93\u5165\u7684 S\uff0c\u79f0\u4f5c S1, S2, ... , Sk \u5176\u4e2d k &gt;= 10\u4ebf\uff0c\u4f60\u9700\u8981\u4f9d\u6b21\u68c0\u67e5\u5b83\u4eec\u662f\u5426\u4e3a T \u7684\u5b50\u5e8f\u5217\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u4f1a\u600e\u6837\u6539\u53d8\u4ee3\u7801\uff1f<br><\/em><em><br><\/em><em> \u81f4\u8c22\uff1a<br><\/em><em><br><\/em><em> \u7279\u522b\u611f\u8c22 @pbrother \u6dfb\u52a0\u6b64\u95ee\u9898\u5e76\u4e14\u521b\u5efa\u6240\u6709\u6d4b\u8bd5\u7528\u4f8b\u3002<br><\/em><em><br><\/em><em><br><\/em><em><br><\/em><em> \u793a\u4f8b 1\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1as = \"abc\", t = \"ahbgdc\"<br><\/em><em> \u8f93\u51fa\uff1atrue<br><\/em><em> \u793a\u4f8b 2\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1as = \"axc\", t = \"ahbgdc\"<br><\/em><em> \u8f93\u51fa\uff1afalse<br><\/em><em><br><\/em><em><br><\/em><em> \u63d0\u793a\uff1a<br><\/em><em><br><\/em><em> 0 &lt;= s.length &lt;= 100<br><\/em><em> 0 &lt;= t.length &lt;= 10^4<br><\/em><em> \u4e24\u4e2a\u5b57\u7b26\u4e32\u90fd\u53ea\u7531\u5c0f\u5199\u5b57\u7b26\u7ec4\u6210\u3002<br><\/em><em><br><\/em><em><br><\/em><em> * <\/em><strong><em>@author <\/em><\/strong><em>zeroheart<br><\/em><em> *<br><\/em><em> *\/<br><\/em>public class Question392 {<br><br>   public boolean isSubsequence(String s, String t) {<br>      \/\/ \u8fd9\u9898\u4e5f\u4e0d\u7528dp\u4e86\u5427\u3002\u3002<br>      int sl = s.length();<br>      int tl = t.length();<br><br>      int i = 0;<br>      int j = 0;<br>      while (i&lt;sl &amp;&amp; j&lt;tl){<br>         if(s.charAt(i) == t.charAt(j)){<br>            i++;<br>            j++;<br>         }else {<br>            j++;<br>         }<br>      }<br><br>      return i == sl;<br><br>   }<br><br>   public static void main(String args[]){<br>   }<br>}<\/pre>\n\n\n\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/longest-common-subsequence\/\">1143. \u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217<\/a><\/p>\n\n\n\n<p>\u8fd9\u4e5f\u662f\u7ecf\u5178\u9898\u76ee<\/p>\n\n\n\n<p>dp[i][j] \u8868\u793a\u7684\u662f0-i\u548c0-j\u7684\u6700\u957f\u516c\u5171\u5b57\u4e32<\/p>\n\n\n\n<p>\u9012\u63a8\u516c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if(text1.charAt(i-1) == text2.charAt(j-1)){\n               dp&#91;i]&#91;j] = dp&#91;i-1]&#91;j-1] + 1;\n            }else {\n               dp&#91;i]&#91;j] = Math.<em>max<\/em>(dp&#91;i-1]&#91;j], dp&#91;i]&#91;j-1]);\n            }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><br><em>\/**<br><\/em><em> *<br><\/em><em> 1143. \u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217<br><\/em><em> \u7ed9\u5b9a\u4e24\u4e2a\u5b57\u7b26\u4e32 text1 \u548c text2\uff0c\u8fd4\u56de\u8fd9\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u6700\u957f \u516c\u5171\u5b50\u5e8f\u5217 \u7684\u957f\u5ea6\u3002\u5982\u679c\u4e0d\u5b58\u5728 \u516c\u5171\u5b50\u5e8f\u5217 \uff0c\u8fd4\u56de 0 \u3002<br><\/em><em><br><\/em><em> \u4e00\u4e2a\u5b57\u7b26\u4e32\u7684 \u5b50\u5e8f\u5217 \u662f\u6307\u8fd9\u6837\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff1a\u5b83\u662f\u7531\u539f\u5b57\u7b26\u4e32\u5728\u4e0d\u6539\u53d8\u5b57\u7b26\u7684\u76f8\u5bf9\u987a\u5e8f\u7684\u60c5\u51b5\u4e0b\u5220\u9664\u67d0\u4e9b\u5b57\u7b26\uff08\u4e5f\u53ef\u4ee5\u4e0d\u5220\u9664\u4efb\u4f55\u5b57\u7b26\uff09\u540e\u7ec4\u6210\u7684\u65b0\u5b57\u7b26\u4e32\u3002<br><\/em><em><br><\/em><em> \u4f8b\u5982\uff0c\"ace\" \u662f \"abcde\" \u7684\u5b50\u5e8f\u5217\uff0c\u4f46 \"aec\" \u4e0d\u662f \"abcde\" \u7684\u5b50\u5e8f\u5217\u3002<br><\/em><em> \u4e24\u4e2a\u5b57\u7b26\u4e32\u7684 \u516c\u5171\u5b50\u5e8f\u5217 \u662f\u8fd9\u4e24\u4e2a\u5b57\u7b26\u4e32\u6240\u5171\u540c\u62e5\u6709\u7684\u5b50\u5e8f\u5217\u3002<br><\/em><em><br><\/em><em><br><\/em><em><br><\/em><em> \u793a\u4f8b 1\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1atext1 = \"abcde\", text2 = \"ace\"<br><\/em><em> \u8f93\u51fa\uff1a3<br><\/em><em> \u89e3\u91ca\uff1a\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u662f \"ace\" \uff0c\u5b83\u7684\u957f\u5ea6\u4e3a 3 \u3002<br><\/em><em> \u793a\u4f8b 2\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1atext1 = \"abc\", text2 = \"abc\"<br><\/em><em> \u8f93\u51fa\uff1a3<br><\/em><em> \u89e3\u91ca\uff1a\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u662f \"abc\" \uff0c\u5b83\u7684\u957f\u5ea6\u4e3a 3 \u3002<br><\/em><em> \u793a\u4f8b 3\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1atext1 = \"abc\", text2 = \"def\"<br><\/em><em> \u8f93\u51fa\uff1a0<br><\/em><em> \u89e3\u91ca\uff1a\u4e24\u4e2a\u5b57\u7b26\u4e32\u6ca1\u6709\u516c\u5171\u5b50\u5e8f\u5217\uff0c\u8fd4\u56de 0 \u3002<br><\/em><em><br><\/em><em><br><\/em><em> \u63d0\u793a\uff1a<br><\/em><em><br><\/em><em> 1 &lt;= text1.length, text2.length &lt;= 1000<br><\/em><em> text1 \u548c text2 \u4ec5\u7531\u5c0f\u5199\u82f1\u6587\u5b57\u7b26\u7ec4\u6210\u3002<br><\/em><em><br><\/em><em> * <\/em><strong><em>@author <\/em><\/strong><em>zeroheart<br><\/em><em> *<br><\/em><em> *\/<br><\/em>public class Question1143 {<br><br>   public int longestCommonSubsequence(String text1, String text2) {<br>      \/\/ dp[i][j] \u8868\u793a\u7684\u662f0-i\u548c0-j\u7684\u6700\u957f\u516c\u5171\u5b57\u4e32<br>      int len1 = text1.length();<br>      int len2 = text2.length();<br><br>      int[][] dp = new int[len1+1][len2+1];<br><br>      for(int i = 1; i&lt;len1; i++){<br>         for(int j = 1; j&lt;len2; j++){<br>            if(text1.charAt(i-1) == text2.charAt(j-1)){<br>               dp[i][j] = dp[i-1][j-1] + 1;<br>            }else {<br>               dp[i][j] = Math.<em>max<\/em>(dp[i-1][j], dp[i][j-1]);<br>            }<br>         }<br>      }<br><br>      return dp[len1][len2];<br>   }<br><br>   public static void main(String args[]){<br>   }<br>}<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>392. \u5224\u65ad\u5b50\u5e8f\u5217 \/** * 392. \u5224\u65ad\u5b50\u5e8f\u5217 \u7ed9\u5b9a\u5b57\u7b26\u4e32 s \u548c t \uff0c\u5224\u65ad s \u662f\u5426\u4e3a t \u7684\u5b50\u5e8f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[155],"tags":[158],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2865"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2865"}],"version-history":[{"count":5,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2865\/revisions"}],"predecessor-version":[{"id":2870,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2865\/revisions\/2870"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}