{"id":197747,"date":"2020-11-04T16:17:44","date_gmt":"2020-11-04T08:17:44","guid":{"rendered":"http:\/\/4563.org\/?p=197747"},"modified":"2020-11-04T16:17:44","modified_gmt":"2020-11-04T08:17:44","slug":"google-%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e5%8d%95%e8%af%8d%e6%8b%86%e5%88%86-iii%ef%bc%88%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92%ef%bc%89","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=197747","title":{"rendered":"Google \u9762\u8bd5\u9898\uff1a\u5355\u8bcd\u62c6\u5206 III\uff08\u52a8\u6001\u89c4\u5212\uff09"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Google \u9762\u8bd5\u9898\uff1a\u5355\u8bcd\u62c6\u5206 III\uff08\u52a8\u6001\u89c4\u5212\uff09               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4eca\u5e74\u5e74\u521d\u9762\u4e86 G,\u9898\u76ee\u627e\u4e86\u9053\u7c7b\u4f3c\u7684\uff0cG \u5bb6\u4e0d\u4e00\u5b9a\u8981\u7ed9\u51fa\u6700\u4f18\u89e3\uff0c\u4e3b\u8981\u662f\u9700\u8981\u6709\u4ee3\u7801\u4f18\u5316\u8fc7\u7a0b\uff0c\u5c55\u73b0\u51fa\u81ea\u5df1\u7684\u903b\u8f91\u601d\u8def\u3002<\/p>\n<p>\u7ed9\u51fa\u4e00\u4e2a\u5355\u8bcd\u8868\u548c\u4e00\u6761\u53bb\u6389\u6240\u6709\u7a7a\u683c\u7684\u53e5\u5b50\uff0c\u6839\u636e\u7ed9\u51fa\u7684\u5355\u8bcd\u8868\u6dfb\u52a0\u7a7a\u683c, \u8fd4\u56de\u53ef\u4ee5\u6784\u6210\u7684\u53e5\u5b50\u7684\u6570\u91cf, \u4fdd\u8bc1\u6784\u6210\u7684\u53e5\u5b50\u4e2d\u6240\u6709\u7684\u5355\u8bcd\u90fd\u53ef\u4ee5\u5728\u5355\u8bcd\u8868\u4e2d\u627e\u5230.\uff08\u5ffd\u7565\u5927\u5c0f\u5199\uff09<\/p>\n<p>\u5728\u7ebf\u8bc4\u6d4b\u5730\u5740<\/p>\n<h2>\u6837\u4f8b 1<\/h2>\n<pre><code>\u8f93\u5165\uff1a \"CatMat\" [\"Cat\", \"Mat\", \"Ca\", \"tM\", \"at\", \"C\", \"Dog\", \"og\", \"Do\"] \u8f93\u51fa\uff1a3 \u89e3\u91ca\uff1a \u6211\u4eec\u53ef\u4ee5\u6709\u5982\u4e0b\u4e09\u79cd\u65b9\u5f0f\uff1a \"CatMat\" = \"Cat\" + \"Mat\" \"CatMat\" = \"Ca\" + \"tM\" + \"at\" \"CatMat\" = \"C\" + \"at\" + \"Mat\" <\/code><\/pre>\n<h2>\u6837\u4f8b 2<\/h2>\n<pre><code>\u8f93\u5165\uff1a \"a\" [] \u8f93\u51fa\uff1a 0 <\/code><\/pre>\n<h2>\u7b97\u6cd5\uff1aDP(\u52a8\u6001\u89c4\u5212)<\/h2>\n<p>\u6839\u636e\u9898\u76ee\u7ed9\u51fa\u7684\u63cf\u8ff0\uff0c\u6211\u4eec\u7a0d\u52a0\u601d\u8003\uff0c\u53ef\u4ee5\u610f\u8bc6\u5230\u5bf9\u4e8e\u53e5\u5b50 s\uff0c\u6211\u4eec\u53ef\u4ee5\u62c6\u5206\u6210 s1 \u548c s2\uff0c\u90a3\u4e48\u6839\u636e\u4e58\u6cd5\u539f\u7406\uff0cs \u7684\u62c6\u5206\u65b9\u6cd5\u6570\u5c31\u7b49\u4e8e s1 \u7684\u62c6\u5206\u65b9\u6cd5\u6570\u4e58\u4ee5 s2 \u7684\u62c6\u5206\u65b9\u6cd5\u6570\uff0c\u7531\u6b64\u6211\u4eec\u60f3\u5230\u52a8\u6001\u89c4\u5212\u3002<\/p>\n<h2>\u7b97\u6cd5\u601d\u8def<\/h2>\n<ul>\n<li>\u6211\u4eec\u5b9a\u4e49\u72b6\u6001 dp[i][j]\u8868\u793a s[i:j+1]\u7684\u62c6\u5206\u65b9\u6cd5\u6570<\/li>\n<li>\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/11\/20201122_5fbaced6bd930.png\" alt=\"Google \u9762\u8bd5\u9898\uff1a\u5355\u8bcd\u62c6\u5206 III\uff08\u52a8\u6001\u89c4\u5212\uff09\" \/><\/p>\n<h2>\u4ee3\u7801\u601d\u8def<\/h2>\n<ol>\n<li>\u672c\u9898\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u6240\u4ee5\u5148\u5c06\u6240\u6709\u5b57\u7b26\u8f6c\u5316\u4e3a\u5c0f\u5199<\/li>\n<li>\u5148\u5c06 dict \u4e2d\u4e0e s \u7684\u5b50\u4e32\u5339\u914d\uff0c\u8ba1\u7b97\u51fa\u521d\u59cb\u7684 dp \u6570\u7ec4<\/li>\n<li>\u679a\u4e3e i \u548c j\uff0c\u5373 s \u7684\u6240\u6709\u5b50\u4e32\uff0c\u518d\u679a\u4e3e\u5206\u5272\u4f4d\u7f6e k \u8ba1\u7b97\u51fa\u6bcf\u4e00\u4e2a\u5b50\u4e32\u7684\u62c6\u5206\u65b9\u6cd5\u6570<\/li>\n<li>\u8fd4\u56de\u7b54\u6848 dp[0][n-1]<\/li>\n<\/ol>\n<h2>\u590d\u6742\u5ea6\u5206\u6790<\/h2>\n<ul>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(N^2)<\/li>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(N^3)<\/li>\n<\/ul>\n<h2>\u4f18\u5316<\/h2>\n<ul>\n<li>\u6211\u4eec\u53ef\u4ee5\u5c06\u72b6\u6001\u5b9a\u4e49\u6210\u4e00\u7ef4\u6570\u7ec4,dp[i]\u8868\u793a s[0:i+1]\u7684\u62c6\u5206\u65b9\u6cd5\u6570<\/li>\n<li>\u8f6c\u79fb\u4e5f\u4e0d\u518d\u662f\u679a\u4e3e\u4e2d\u95f4\u7684\u5206\u5272\u4f4d\u7f6e\uff0c\u800c\u662f\u679a\u4e3e\u63a5\u5728 s[0:i+1]\u540e\u9762\u7684\u4e32<\/li>\n<li>\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/11\/20201122_5fbacee3eaf45.png\" alt=\"Google \u9762\u8bd5\u9898\uff1a\u5355\u8bcd\u62c6\u5206 III\uff08\u52a8\u6001\u89c4\u5212\uff09\" \/><\/p>\n<h2>\u4f18\u5316\u540e\u590d\u6742\u5ea6\u5206\u6790<\/h2>\n<ul>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(N)<\/li>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(N^2 logN) \u5224\u65ad\u4e00\u4e2a\u5b50\u4e32\u662f\u5426\u5b58\u5728\u4e8e dict \u4f7f\u7528 hash \u5224\u65ad\uff0c\u9700\u8981 logN \u7684\u590d\u6742\u5ea6<\/li>\n<\/ul>\n<pre><code>public class Solution {     \/*      * @param : A string      * @param : A set of word      * @return: the number of possible sentences.      *\/     public int wordBreak3(String s, Set&lt;String&gt; dict) {         int n = s.length();         String lowerS = s.toLowerCase();         Set&lt;String&gt; lowerDict = new HashSet&lt;String&gt;();         for(String str : dict) {             lowerDict.add(str.toLowerCase());         }         int[][] dp = new int[n][n];         for(int i = 0; i &lt; n; i++){             for(int j = i; j &lt; n;j++){                 String sub = lowerS.substring(i, j + 1);                 if(lowerDict.contains(sub)){                     dp[i][j] = 1;                 }             }         }         for(int i = 0; i &lt; n; i++){             for(int j = i; j &lt; n; j++){                 for(int k = i; k &lt; j; k++){                     dp[i][j] += (dp[i][k] * dp[k + 1][j]);                 }             }         }         return dp[0][n - 1];     } }  # \u4f18\u5316 public class Solution {     \/*      * @param : A string      * @param : A set of word      * @return: the number of possible sentences.      *\/     public int wordBreak3(String s, Set&lt;String&gt; dict) {         if (s == null ||s.length() == 0 || dict == null || dict.size() == 0) {             return 0;         }          \/\/\u5c06\u5b57\u7b26\u5168\u90e8\u8f6c\u5316\u4e3a\u5c0f\u5199\uff0c\u5e76\u5c06 dict \u8f6c\u6362\u6210 hash_stet \u5b58\u50a8\uff0c\u964d\u4f4e\u5224\u65ad\u5b50\u4e32\u5b58\u5728\u6027\u7684\u65f6\u95f4\u590d\u6742\u5ea6         s = s.toLowerCase();         Set&lt;String&gt; set = new HashSet&lt;String&gt;();         for (String word : dict) {             String str = word.toLowerCase();             set.add(str);         }          \/\/dp[i]\u8868\u793a s[0:i](\u4e0d\u542b s[i])\u7684\u62c6\u5206\u65b9\u6cd5\u6570         int len = s.length();         int[] dp = new int[len + 1];          \/\/dp[0]\u8868\u793a\u7a7a\u4e32\u7684\u62c6\u5206\u65b9\u6cd5\u6570         dp[0] = 1;                   for (int i = 0; i &lt; len; i++) {             for (int j = i; j &lt; len; j++) {                 \/\/\u82e5\u5b58\u5728\u5339\u914d\uff0c\u5219\u8fdb\u884c\u72b6\u6001\u8f6c\u79fb                 if (set.contains(s.substring(i, j + 1))) {                     dp[j + 1] += dp[i];                 }             }         }         return dp[len];     } } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Google \u9762\u8bd5\u9898\uff1a\u5355\u8bcd\u62c6\u5206 I&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/197747"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=197747"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/197747\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=197747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=197747"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=197747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}