{"id":4406,"date":"2018-12-08T10:23:00","date_gmt":"2018-12-08T18:23:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4406"},"modified":"2018-12-08T13:24:53","modified_gmt":"2018-12-08T21:24:53","slug":"leetcode-936-stamping-the-sequence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-936-stamping-the-sequence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 936. Stamping The Sequence"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 936. Stamping The Sequence - \u5237\u9898\u627e\u5de5\u4f5c EP236\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/qTDYfhuqCVg?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><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>You want to form a\u00a0<code>target<\/code>\u00a0string of\u00a0<strong>lowercase letters<\/strong>.<\/p>\n<p>At the beginning, your sequence is\u00a0<code>target.length<\/code>\u00a0<code>'?'<\/code>\u00a0marks.\u00a0 You also have a\u00a0<code>stamp<\/code>\u00a0of lowercase letters.<\/p>\n<p>On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.\u00a0 You can make up to\u00a0<code>10 * target.length<\/code>\u00a0turns.<\/p>\n<p>For example, if the initial sequence is\u00a0<span style=\"font-family: monospace;\">&#8220;?????&#8221;<\/span>, and your stamp is\u00a0<code>\"abc\"<\/code>,\u00a0 then you may make\u00a0<span style=\"font-family: monospace;\">&#8220;abc??&#8221;, &#8220;?abc?&#8221;, &#8220;??abc&#8221;\u00a0<\/span>in the first turn.\u00a0 (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.)<\/p>\n<p>If the sequence is possible to stamp, then return an array of\u00a0the index of the left-most letter being stamped at each turn.\u00a0 If the sequence is not possible to stamp, return an empty array.<\/p>\n<p>For example, if the sequence is\u00a0<span style=\"font-family: monospace;\">&#8220;ababc&#8221;<\/span>, and the stamp is\u00a0<code>\"abc\"<\/code>, then we could return the answer\u00a0<code>[0, 2]<\/code>, corresponding to the moves\u00a0<span style=\"font-family: monospace;\">&#8220;?????&#8221; -&gt; &#8220;abc??&#8221; -&gt; &#8220;ababc&#8221;<\/span>.<\/p>\n<p>Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within\u00a0<code>10 * target.length<\/code>\u00a0moves.\u00a0 Any answers specifying more than this number of moves\u00a0will not be accepted.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>stamp = <span id=\"example-input-1-1\">\"abc\"<\/span>, target = <span id=\"example-input-1-2\">\"ababc\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[0,2]<\/span>\r\n([1,0,2] would also be accepted as an answer, as well as some other answers.)\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong>stamp = <span id=\"example-input-2-1\">\"<\/span><span id=\"example-input-2-2\">abca<\/span>\", target = <span id=\"example-input-2-2\">\"<\/span>aabcaca\"\r\n<strong>Output: <\/strong><span id=\"example-output-2\">[3,0,1]<\/span><\/pre>\n<div>\n<p><strong>Note:<\/strong><\/p>\n<\/div>\n<\/div>\n<ol>\n<li><code>1 &lt;= stamp.length &lt;= target.length &lt;= 1000<\/code><\/li>\n<li><code>stamp<\/code>\u00a0and\u00a0<code>target<\/code>\u00a0only contain lowercase letters.<\/li>\n<\/ol>\n<h1><strong>Solution: Greedy + Reverse Simulation<\/strong><\/h1>\n<p>Reverse the stamping process. Each time find a full or partial match. Replace the matched char to &#8216;?&#8217;.<\/p>\n<p>Don&#8217;t forget the reverse the answer as well.<\/p>\n<p>T = &#8220;ababc&#8221;, S = &#8220;abc&#8221;<\/p>\n<p>T = &#8220;ab???&#8221;, index = 2<\/p>\n<p>T = &#8220;?????&#8221;, index = 0<\/p>\n<p>ans = [0, 2]<\/p>\n<p>Time complexity: O((T &#8211; S)*S)<\/p>\n<p>Space complexity: O(T)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua, running time: 12 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; movesToStamp(string stamp, string target) {\r\n    vector&lt;int&gt; ans;\r\n    vector&lt;int&gt; seen(target.length());\r\n    int total = 0;\r\n    while (total &lt; target.length()) {      \r\n      bool found = false;\r\n      for (int i = 0; i &lt;= target.length() - stamp.length(); ++i) {\r\n        if (seen[i]) continue;\r\n        int l = unStamp(stamp, target, i);\r\n        if (l == 0) continue;\r\n        seen[i] = 1;\r\n        total += l;\r\n        ans.push_back(i);\r\n        found = true;\r\n      }\r\n      if (!found) return {};\r\n    }\r\n    reverse(begin(ans), end(ans));\r\n    return ans;\r\n  }\r\nprivate:\r\n  int unStamp(const string&amp; stamp, string&amp; target, int s) {    \r\n    int l = stamp.size();\r\n    for (int i = 0; i &lt; stamp.length(); ++i) {\r\n      if (target[s + i] == '?')\r\n        --l;\r\n      else if (target[s + i] != stamp[i])\r\n        return 0;\r\n    }\r\n    \r\n    if (l != 0)\r\n      std::fill(begin(target) + s, begin(target) + s + stamp.length(), '?');\r\n    return l;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem You want to form a\u00a0target\u00a0string of\u00a0lowercase letters. At the beginning, your sequence is\u00a0target.length\u00a0&#8216;?&#8217;\u00a0marks.\u00a0 You also have a\u00a0stamp\u00a0of lowercase letters. On each turn, you may&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,217,410,424,179],"class_list":["post-4406","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-hard","tag-matching","tag-pattern","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4406","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=4406"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4406\/revisions"}],"predecessor-version":[{"id":4413,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4406\/revisions\/4413"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}