{"id":316334,"date":"2021-02-04T18:38:33","date_gmt":"2021-02-04T10:38:33","guid":{"rendered":"http:\/\/4563.org\/?p=316334"},"modified":"2021-02-04T18:38:33","modified_gmt":"2021-02-04T10:38:33","slug":"%e5%ad%97%e8%8a%82%e8%b7%b3%e5%8a%a8%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%9f%a5%e6%89%be-ii","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=316334","title":{"rendered":"\u5b57\u8282\u8df3\u52a8\u9762\u8bd5\u9898\uff1a\u5b57\u7b26\u4e32\u67e5\u627e II"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5b57\u8282\u8df3\u52a8\u9762\u8bd5\u9898\uff1a\u5b57\u7b26\u4e32\u67e5\u627e II               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h3>\u63cf\u8ff0<\/h3>\n<p>\u5b9e\u73b0\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n + m)\u7684\u65b9\u6cd5 strStr \u3002 strStr \u8fd4\u56de\u76ee\u6807\u5b57\u7b26\u4e32\u5728\u6e90\u5b57\u7b26\u4e32\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684\u4f4d\u7f6e. \u76ee\u6807\u5b57\u4e32\u7684\u957f\u5ea6\u4e3a m , \u6e90\u5b57\u4e32\u7684\u957f\u5ea6\u4e3a n . \u5982\u679c\u76ee\u6807\u5b57\u4e32\u4e0d\u5728\u6e90\u5b57\u4e32\u4e2d\u5219\u8fd4\u56de -1 \u3002<\/p>\n<p>\u5728\u7ebf\u8bc4\u6d4b\u5730\u5740<\/p>\n<h3>\u6837\u4f8b 1<\/h3>\n<pre><code>\u8f93\u5165\uff1asource = \"abcdef\"\uff0ctarget = \"bcd\" \u8f93\u51fa\uff1a1 \u89e3\u91ca\uff1a \u5b57\u7b26\u4e32\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u4e3a 1 \u3002 <\/code><\/pre>\n<h3>\u6837\u4f8b 2<\/h3>\n<pre><code>\u8f93\u5165\uff1asource = \"abcde\"\uff0ctarget = \"e\" \u8f93\u51fa\uff1a4 \u89e3\u91ca\uff1a \u5b57\u7b26\u4e32\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u4e3a 4 \u3002 <\/code><\/pre>\n<h3>\u7b97\u6cd5\uff1aHASH<\/h3>\n<ul>\n<li>\u5b57\u7b26\u4e32 Hash \u53ef\u4ee5\u901a\u4fd7\u7684\u7406\u89e3\u4e3a\uff0c\u628a\u4e00\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6574\u6570\u3002<\/li>\n<li>\u5982\u679c\u6211\u4eec\u901a\u8fc7\u67d0\u79cd\u65b9\u6cd5\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6574\u6570\uff0c\u5c31\u53ef\u4ee5\u5feb\u901f\u7684\u5224\u65ad\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u540c\u3002<\/li>\n<li>\u5f53\u7136\u5982\u679c\u6709\u4e0d\u540c\u7684\u4e24\u4e2a\u5b57\u7b26\u4e32\u540c\u65f6 Hash \u5230\u4e00\u4e2a\u6574\u6570\uff0c\u8fd9\u6837\u5c31\u6bd4\u8f83\u9ebb\u70e6\u4e86\uff0c\u6240\u4ee5\u6211\u4eec\u5e0c\u671b\u6784\u9020\u8fd9\u4e2a Hash \u51fd\u6570\u4f7f\u5f97\u4ed6\u4eec\u6210\u4e3a\u4e00\u4e2a\u5355\u5c04\u3002<\/li>\n<\/ul>\n<h3>\u7b97\u6cd5\u601d\u8def<\/h3>\n<ul>\n<li>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32 S,\u5bf9\u4e8e\u4e00\u4e2a\u5b57\u7b26 c \u6211\u4eec\u89c4\u5b9a id(c)=c-&#8216;a&#8217;+1<\/li>\n<li>hash[i]=(hash[i-1]*p+id(s[i]))%MOD<\/li>\n<li>p \u548c MOD \u5747\u4e3a\u8d28\u6570\uff0c\u5e76\u4e14\u8981\u5c3d\u91cf\u5927<\/li>\n<\/ul>\n<h3>\u4ee3\u7801\u601d\u8def<\/h3>\n<ol>\n<li>\u8ba1\u7b97 target \u7684 hash \u503c<\/li>\n<li> \u8ba1\u7b97 source \u7684 hash \u503c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f9d\u6b21\u8ba1\u7b97\u6bcf targetLen \u4f4d\u7684 hash \u503c\u3002<br \/>\n<blockquote>\n<p>\u5047\u8bbe target \u957f\u5ea6\u4e3a 2\uff0csource \u4e3a\u201cabcd\u201d<\/p>\n<p>hash(&#8220;cd&#8221;) = (hash(&#8220;bc + d&#8221;) &#8211; hash(&#8220;b&#8221;)*2 ) % BASE<\/p>\n<\/blockquote>\n<\/li>\n<\/ol>\n<h3>\u590d\u6742\u5ea6\u5206\u6790<\/h3>\n<p>N \u8868\u793a\u5b57\u7b26\u4e32 source \u957f\u5ea6\uff0cM \u8868\u793a\u5b57\u7b26\u4e32 target \u957f\u5ea6<\/p>\n<ul>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(1)<\/li>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(N+M)<\/li>\n<\/ul>\n<pre><code>public class Solution {      private static final Integer BASE = 100007;      \/*       * @param source: A source string       * @param target: A target string       * @return: An integer as index       *\/      public int strStr2(String source, String target) {          if (source == null || target == null) {              return -1;          }                    int m = target.length();          if (m == 0) {              return 0;          }                    int power = 1;          for (int i = 0; i &lt; m; i++) {              power = (power * 31) % BASE;          }          \/\/\u5148\u8ba1\u7b97\u4e00\u4e0b target \u7684 hash \u503c          int targetCode = 0;          for (int i = 0; i &lt; m; i++) {              targetCode = (targetCode * 31 + target.charAt(i)) % BASE;          }          \/\/\u5f53 source code \u52a0\u4e0a\u53f3\u8fb9\u4e00\u4e2a character\uff0c\u5c31\u8981\u51cf\u6389\u5de6\u8fb9\u7684\u4e00\u4e2a character          int sourceCode = 0;          for (int i = 0; i &lt; source.length(); i++) {              sourceCode = (sourceCode * 31 + source.charAt(i)) % BASE;              if (i &lt;= m - 1) {                  continue;              }                            sourceCode = (sourceCode - power * source.charAt(i - m)) % BASE;              if (sourceCode &lt; 0) {                  sourceCode += BASE;              }              \/\/\u82e5 hash \u503c\u76f8\u540c\uff0c\u8fd4\u56de\u7b54\u6848              if (sourceCode == targetCode) {                  return i - m + 1;              }          }          return -1;      }  } <\/code><\/pre>\n<p>\u66f4\u591a\u9898\u89e3\u53c2\u8003<\/p>\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>\u5b57\u8282\u8df3\u52a8\u9762\u8bd5\u9898\uff1a\u5b57\u7b26\u4e32\u67e5\u627e II &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\/316334"}],"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=316334"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/316334\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=316334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=316334"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=316334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}