{"id":2007,"date":"2016-04-25T14:30:16","date_gmt":"2016-04-25T06:30:16","guid":{"rendered":"http:\/\/boweihe.me\/?p=2007"},"modified":"2016-04-25T14:30:16","modified_gmt":"2016-04-25T06:30:16","slug":"leetcode-139-word-break","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2007","title":{"rendered":"LeetCode 139. Word Break"},"content":{"rendered":"<h3>\u9898\u76ee<\/h3>\n<p>Given a string <i>s<\/i> and a dictionary of words <i>dict<\/i>, determine if <i>s<\/i> can be segmented into a space-separated sequence of one or more dictionary words.<br \/>\nFor example, given<br \/>\n<i>s<\/i> = <code>\"leetcode\"<\/code>,<br \/>\n<i>dict<\/i> = <code>[\"leet\", \"code\"]<\/code>.<br \/>\nReturn true because <code>\"leetcode\"<\/code> can be segmented as <code>\"leet code\"<\/code>.<\/p>\n<h3>\u601d\u8def1<\/h3>\n<p>\u5199\u7684\u7b2c\u4e00\u4e2a\u7248\u672c\u662f\u8d85\u65f6\u7684\uff0c\u4f46\u662f\u601d\u8def\u5e94\u5f53\u662f\u6b63\u786e\u7684\uff0c\u53cd\u6b63\u5c31\u662f\u52a8\u6001\u89c4\u5212\uff0c\u4f46\u6211\u7528\u7684\u662f\u9012\u5f52\u65b9\u6cd5\u3002<br \/>\n\u5c31\u662f\u5982\u679c\u627e\u5230s[start,end]\u662f\u5355\u8bcd\u8868\u4e2d\u7684\u5355\u8bcd\u65f6\uff0c\u6709\u4e24\u79cd\u5904\u7406\uff0c\u4e00\u79cd\u662f\u7b97\u5165\u7136\u540e\u5f80\u540e\u8d70\uff1b\u53e6\u4e00\u79cd\u662f\u5ffd\u7565\u8fd9\u6b21\u5339\u914d\u6210\u529f\uff0c\u7ee7\u7eed\u5f80\u540e\u5bfb\u627e\u3002<br \/>\n\u4ee3\u7801\u5927\u6982\u8fd9\u6837\uff1a<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n\tint findNextWord(const string&amp; s, int start, int curr, unordered_set&lt;string&gt;&amp; wordDict) {\n\t\t\/\/Find the end position of next possible word, or return -1\n\t\tstring word = s.substr(start, curr - start + 1);\n\t\tfor (int i = curr + 1; i &lt; s.size(); i++) {\n\t\t\tword += s[i];\n\t\t\tif (wordDict.count(word) &gt; 0) {\n\t\t\t\treturn i;\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t}\n\tbool parseSeq(const string&amp; s, unordered_set&lt;string&gt;&amp; wordDict, int start, int end, map&lt;int, bool&gt;&amp; cache) {\n\t\t\/\/Judge s[start...end]\n\t\tif (end &gt;= s.size())\n\t\t\treturn false;\n\t\tif (cache.count(start) &gt; 1)\n\t\t\treturn cache[start];\n\t\tstring word = s.substr(start, end - start + 1);\n\t\tif (wordDict.count(word) &gt; 0) {\n\t\t\tbool withThisWord = parseSeq(s, wordDict, end + 1, end + 1, cache);\n\t\t\tif (end + 1 == s.size() || withThisWord) {\n\t\t\t\tcache[start] = true;\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\tint nextPos = findNextWord(s, start, end, wordDict);\n\t\tif (-1 != nextPos) {\n\t\t\tbool appendWord = parseSeq(s, wordDict, start, nextPos, cache);\n\t\t\tif (appendWord) {\n\t\t\t\tcache[start] = true;\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\tcache[start] = false;\n\t\treturn false;\n\t}\n\tbool wordBreak(string s, unordered_set&lt;string&gt;&amp; wordDict) {\n\t\tmap&lt;int, bool&gt; cache;\n\t\treturn parseSeq(s, wordDict, 0, 0, cache);\n\t}\n};<\/pre>\n<p>\u4f46\u662f\u5982\u679c\u8fd9\u91cc\u51fa\u73b0\u4e86\u8d85\u7ea7\u591a\u7684\u5355\u5b57\uff0c\u6bd4\u5982\u5b57\u7b26\u4e32\u662f<\/p>\n<pre class=\"lang:default decode:true \">aaaaaaaaaaaaaaaaaaaaaaa<\/pre>\n<p>\u7136\u540e\u5b57\u5178\u662f<\/p>\n<pre class=\"lang:default decode:true \">{a,aa,aaa,aaaa}<\/pre>\n<p>\u8fd9\u5c31\u641e\u7b11\u4e86\uff0c\u5f97\u5339\u914d\u6b7b\uff0c\u6240\u4ee5\u80af\u5b9a\u662f\u8d85\u65f6\u3002<\/p>\n<h3>\u601d\u8def2<\/h3>\n<p>\u8fd9\u4e2a\u662f\u8ba8\u8bba\u7ec4\u91cc\u770b\u6765\u7684\uff0c\u7a81\u7136\u53d1\u73b0\u4e00\u822c\u6765\u8bf4DP\u9012\u5f52\u80fd\u505a\u7684\uff0c\u5c31\u53ef\u4ee5\u4ece\u540e\u5f80\u524d\u7528\u975e\u9012\u5f52\u7684\u65b9\u6cd5\u5199\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u95ee\u9898\u6765\u8bf4\uff0c\u4ece\u540e\u5f80\u524d\u5e76\u4e0d\u50cf\u505aLCS\u90a3\u6837\u65f6\u8981\u5b8c\u5168\u5012\u7740\u5199\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6709\u5b57\u7b26\u4e32str=<span class=\"lang:default decode:true crayon-inline\">thisisawesome<\/span>\u00a0\u548c\u5b57\u5178<span class=\"lang:default decode:true crayon-inline \">{this, is, a, we, some, awesome}<\/span>\u00a0\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u7528\u4e00\u4e2a\u7f13\u5b58cache\u6765\u8bb0\u5f55\u662f\u5426\u5728str[i]\u4f4d\u7f6e\u6709\u5355\u8bcd\u7ed3\u675f\uff0c\u6bd4\u5982str[3]\u4f4d\u7f6e\u5c31\u662f&#8221;this&#8221;\u8fd9\u4e2a\u8bcd\u7ed3\u675f\u7684\u5730\u65b9\u3002\u5f53\u987a\u7740\u7ee7\u7eed\u641c\u7d22\u7684\u65f6\u5019\uff0c\u5f80\u524d\u67e5\u627e\u7f13\u5b58\u8bb0\u5f55\uff0c\u53ef\u4ee5\u77e5\u9053\u5982\u679ccache[i]=true\u7684\u8bdd\uff0c\u90a3<strong>\u624d\u6709\u53ef\u80fd<\/strong>\u4ecei\u8fd9\u4e2a\u4f4d\u7f6e\u5f80\u540e\u5230\u5f53\u524d\u4f4d\u7f6e\u7ec4\u6210\u65b0\u5355\u8bcd\uff0c\u63a5\u7740\u53bb\u67e5\u627e\u5b57\u5178\u770b\u770b\u6709\u6ca1\u6709\u8fd9\u4e2a\u5355\u8bcd\uff0c\u5982\u679c\u6ca1\u6709\u7684\u8bdd\uff0c\u5c31\u5f97\u7ee7\u7eed\u5f80\u524d\u627e\u5230\u65b0\u7684\u53ef\u7528\u7684\u5f00\u5934i\u3002\u8fd9\u91cc\u52a0\u9ed1\u7684\u201c\u624d\u6709\u53ef\u80fd\u201d\u662f\u6bd4\u8f83\u5173\u952e\u7684\u3002<br \/>\n\u4ee3\u7801\u5927\u6982\u662f\u8fd9\u6837<\/p>\n<pre class=\"lang:c++ decode:true \">bool wordBreak(string s, unordered_set&lt;string&gt;&amp; wordDict) {\n\t\tif (wordDict.empty())\n\t\t\treturn false;\n\t\tvector&lt;bool&gt; cache(s.size() + 1, false); \/\/If a word ends at i, cache[i] = true\n\t\tcache[0] = true;\n\t\tfor (int i = 0; i &lt;= s.size(); i++) {\n\t\t\tfor (int j = i - 1; j &gt;= 0; j--) {\n\t\t\t\tif (cache[j]) {\n\t\t\t\t\t\/\/Look backward and start at the end place of previous word\n\t\t\t\t\tstring word = s.substr(j, i - j); \/\/ s[j...i-1]\n\t\t\t\t\tif (wordDict.find(word) != wordDict.end()) {\n\t\t\t\t\t\tcache[i] = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn cache[s.size()];\n\t}<\/pre>\n<p>\u8fd9\u91cc\u9762\u5411\u524d\u8fed\u4ee3\u4e0d\u65ad\u5bfb\u627ecache[i]\u5e76\u5c1d\u8bd5\u7684\u60c5\u51b5\uff0c\u5bf9\u5e94\u4e8e\u4e4b\u524d\u601d\u8def1\u91cc\u9762\u627e\u5230\u53ef\u7528\u5355\u8bcd\u540e\u7684\u51b3\u5b9a\u662f\u5426\u4f7f\u7528\u7684\u5206\u652f\u60c5\u51b5\u3002\u4f46\u662f\u7531\u4e8e\u51cf\u5c11\u4e86\u4e00\u5927\u5806\u9012\u5f52\u8c03\u7528\uff0c\u6240\u4ee5\u5b9e\u9645\u60c5\u51b5\u4e0b\u7684\u590d\u6742\u5ea6\u964d\u4f4e\u4e86\u4e0d\u5c11\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = &#8220;leetcode&#8221;, dict = [&#8220;leet&#8221;, &#8220;code&#8221;]. Return true because &#8220;leetcode&#8221; can be segmented as &#8220;leet code&#8221;. \u601d\u8def1 \u5199\u7684\u7b2c\u4e00\u4e2a\u7248\u672c\u662f\u8d85\u65f6\u7684\uff0c\u4f46\u662f\u601d\u8def\u5e94\u5f53\u662f\u6b63\u786e\u7684\uff0c\u53cd\u6b63\u5c31\u662f\u52a8\u6001\u89c4\u5212\uff0c\u4f46\u6211\u7528\u7684\u662f\u9012\u5f52\u65b9\u6cd5\u3002 \u5c31\u662f\u5982\u679c\u627e\u5230s[start,end]\u662f\u5355\u8bcd\u8868\u4e2d\u7684\u5355\u8bcd\u65f6\uff0c\u6709\u4e24\u79cd\u5904\u7406\uff0c\u4e00\u79cd\u662f\u7b97\u5165\u7136\u540e\u5f80\u540e\u8d70\uff1b\u53e6\u4e00\u79cd\u662f\u5ffd\u7565\u8fd9\u6b21\u5339\u914d\u6210\u529f\uff0c\u7ee7\u7eed\u5f80\u540e\u5bfb\u627e\u3002 \u4ee3\u7801\u5927\u6982\u8fd9\u6837\uff1a class Solution { public: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66,141,151],"class_list":["post-2007","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj","tag-141","tag-151"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2007"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2007\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}