{"id":5229,"date":"2019-06-16T00:59:27","date_gmt":"2019-06-16T07:59:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5229"},"modified":"2019-06-16T22:49:19","modified_gmt":"2019-06-17T05:49:19","slug":"leetcode-1092-shortest-common-supersequence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1092-shortest-common-supersequence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1092. Shortest Common Supersequence"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1092.  Shortest Common Supersequence \u5237\u9898\u627e\u5de5\u4f5c EP251\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/rfV2BJp8YA8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given two strings&nbsp;<code>str1<\/code>&nbsp;and&nbsp;<code>str2<\/code>,&nbsp;return the shortest string that has both&nbsp;<code>str1<\/code>&nbsp;and&nbsp;<code>str2<\/code>&nbsp;as subsequences.&nbsp;&nbsp;If multiple answers exist, you may return any of them.<\/p>\n\n\n\n<p><em>(A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen&nbsp;anywherefrom T) results in the string S.)<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input: <\/strong>str1 = \"abac\", str2 = \"cab\"\n<strong>Output: <\/strong>\"cabac\"\n<strong>Explanation: <\/strong>\nstr1 = \"abac\" is a substring of \"cabac\" because we can delete the first \"c\".\nstr2 = \"cab\" is a substring of \"cabac\" because we can delete the last \"ac\".\nThe answer provided is the shortest such string that satisfies these properties.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= str1.length, str2.length &lt;= 1000<\/code><\/li><li><code>str1<\/code>&nbsp;and&nbsp;<code>str2<\/code>&nbsp;consist of lowercase English letters.<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/06\/1092-ep251.png\" alt=\"\" class=\"wp-image-5232\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/06\/1092-ep251.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/06\/1092-ep251-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/06\/1092-ep251-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: LCS <\/strong><\/h2>\n\n\n\n<p>Find the LCS (longest common sub-sequence) of two strings, and insert unmatched characters into the LCS.<\/p>\n\n\n\n<p>Time complexity: O(mn)<br>Space complexity: O(mn)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, running time: 16 ms, 15.3 MB\nclass Solution {\npublic:\n  string shortestCommonSupersequence(string str1, string str2) {\n    int l1 = str1.length();\n    int l2 = str2.length();    \n    vector<vector<int>> dp(l1 + 1, vector<int>(l2 + 1));    \n    for (int i = 1; i <= l1; ++i)\n      for (int j = 1; j <= l2; ++j)\n        dp[i][j] = str1[i - 1] == str2[j - 1] ? \n                     1 + dp[i - 1][j - 1] : \n                     max(dp[i - 1][j], dp[i][j - 1]);\n    deque<char> ans;\n    while (l1 || l2) {\n      char c;\n      if (l1 == 0) c = str2[--l2];\n      else if (l2 == 0) c = str1[--l1];\n      else if (str1[l1 - 1] == str2[l2 - 1]) c = str1[--l1] = str2[--l2];\n      else if (dp[l1 - 1][l2] == dp[l1][l2]) c = str1[--l1];\n      else if (dp[l1][l2 - 1] == dp[l1][l2]) c = str2[--l2];\n      ans.push_front(c);\n    }    \n    return {begin(ans), end(ans)};\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two strings&nbsp;str1&nbsp;and&nbsp;str2,&nbsp;return the shortest string that has both&nbsp;str1&nbsp;and&nbsp;str2&nbsp;as subsequences.&nbsp;&nbsp;If multiple answers exist, you may return any of them. (A string S is a subsequence&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,217,66,229],"class_list":["post-5229","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","tag-lcs","tag-subsequence","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5229"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5229\/revisions"}],"predecessor-version":[{"id":5233,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5229\/revisions\/5233"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}