{"id":152202,"date":"2020-09-07T05:05:11","date_gmt":"2020-09-06T21:05:11","guid":{"rendered":"http:\/\/4563.org\/?p=152202"},"modified":"2020-09-08T01:09:46","modified_gmt":"2020-09-07T17:09:46","slug":"%e5%b7%a8%e7%a1%ac%e5%ae%b6%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%ef%bc%88%e5%b8%a6%e8%a7%a3%e9%a2%98%e6%80%9d%e8%b7%af%ef%bc%89","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=152202","title":{"rendered":"\u5de8\u786c\u5bb6\u9762\u8bd5\u9898\uff1a\u5b57\u7b26\u4e32\u67e5\u627e\uff08\u5e26\u89e3\u9898\u601d\u8def\uff09"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5de8\u786c\u5bb6\u9762\u8bd5\u9898\uff1a\u5b57\u7b26\u4e32\u67e5\u627e\uff08\u5e26\u89e3\u9898\u601d\u8def\uff09               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 18<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u7684 source \u5b57\u7b26\u4e32\u548c\u4e00\u4e2a target \u5b57\u7b26\u4e32\uff0c\u4f60\u5e94\u8be5\u5728 source \u5b57\u7b26\u4e32\u4e2d\u627e\u51fa target \u5b57\u7b26\u4e32\u51fa\u73b0\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e(\u4ece 0 \u5f00\u59cb)\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de -1 \u3002<\/p>\n<h2>\u8bf4\u660e<\/h2>\n<p>\u5728\u9762\u8bd5\u4e2d\u6211\u662f\u5426\u9700\u8981\u5b9e\u73b0 KMP \u7b97\u6cd5\uff1f<\/p>\n<ul>\n<li>\u4e0d\u9700\u8981\uff0c\u5f53\u8fd9\u79cd\u95ee\u9898\u51fa\u73b0\u5728\u9762\u8bd5\u4e2d\u65f6\uff0c\u9762\u8bd5\u5b98\u5f88\u53ef\u80fd\u53ea\u662f\u60f3\u8981\u6d4b\u8bd5\u4e00\u4e0b\u4f60\u7684\u57fa\u7840\u5e94\u7528\u80fd\u529b\u3002\u5f53\u7136\u4f60\u9700\u8981\u5148\u8ddf\u9762\u8bd5\u5b98\u786e\u8ba4\u6e05\u695a\u8981\u600e\u4e48\u5b9e\u73b0\u8fd9\u4e2a\u9898\u3002<\/li>\n<li>\u300a\u4e5d\u7ae0\u7b97\u6cd5\u73ed\u300b\u4e2d\u8bb2\u8fc7\u6bd4 KMP \u66f4\u7b80\u5355\u7684\u7b97\u6cd5\uff1aRabin-karp \u7b97\u6cd5<\/li>\n<\/ul>\n<p>\u5728\u7ebf\u505a\u9898\u5730\u5740<\/p>\n<h2>\u6837\u4f8b 1:<\/h2>\n<pre><code>\u8f93\u5165: source = \"source\" \uff0ctarget = \"target\" \u8f93\u51fa:-1  \u6837\u4f8b\u89e3\u91ca: \u5982\u679c source \u91cc\u6ca1\u6709\u5305\u542b target \u7684\u5185\u5bb9\uff0c\u8fd4\u56de-1 <\/code><\/pre>\n<h2>\u6837\u4f8b 2:<\/h2>\n<pre><code>\u8f93\u5165: source = \"abcdabcdefg\" \uff0ctarget = \"bcd\" \u8f93\u51fa: 1  \u6837\u4f8b\u89e3\u91ca: \u5982\u679c source \u91cc\u5305\u542b target \u7684\u5185\u5bb9\uff0c\u8fd4\u56de target \u5728 source \u91cc\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e <\/code><\/pre>\n<h1>[\u9898\u89e3]<\/h1>\n<p> <\/p>\n<p>\u7b97\u6cd5\uff1a\u6a21\u62df<\/p>\n<h1>\u7b97\u6cd5\u601d\u8def<\/h1>\n<p> <\/p>\n<p>\u56e0\u4e3a\u672c\u9898\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6bd4\u8f83\u5bbd\u9650\uff0c\u4e0d\u9700\u8981\u66f4\u4e3a\u590d\u6742\u7684 KMP \u7b97\u6cd5\uff0c\u53ef\u4ee5\u76f4\u63a5\u6a21\u62df\uff0c\u9010\u4e00\u6bd4\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u3002<\/p>\n<h1>\u4ee3\u7801\u601d\u8def<\/h1>\n<p> <\/p>\n<ul>\n<li>\u5148\u8fdb\u884c\u7279\u5224\uff0c\u82e5 target \u957f\u5ea6\u4e3a 0\uff0c\u5219\u8fd4\u56de 0 \uff1b\u82e5 target \u7684\u957f\u5ea6\u5927\u4e8e source\uff0c\u5219\u8868\u793a\u4e0d\u53ef\u80fd\u5339\u914d\uff0c\u8fd4\u56de-1<\/li>\n<li>\u679a\u4e3e\u5339\u914d source \u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u4ece 0 \u5230 sourceLen-targetLen<\/li>\n<li>\u5bf9\u4e8e\u4e00\u4e2a\u8d77\u59cb\u4f4d\u7f6e\uff0c\u6bd4\u5bf9\u4e4b\u540e\u7684 targetLen \u4f4d\u662f\u5426\u76f8\u540c\uff0c\u82e5\u9047\u5230\u4e0d\u76f8\u540c\u76f4\u63a5\u9000\u51fa\u5224\u65ad\uff0c\u5c1d\u8bd5\u4e0b\u4e00\u4e2a\u8d77\u59cb\u4f4d\u7f6e<\/li>\n<li>\u5f53\u6210\u529f\u6bd4\u5bf9\u5b8c targetLen \u4f4d\u540e\uff0c\u8bf4\u660e\u5168\u90e8\u76f8\u540c\uff0c\u5219\u8fd4\u56de\u8d77\u59cb\u4f4d\u7f6e<\/li>\n<\/ul>\n<h1>\u590d\u6742\u5ea6\u5206\u6790<\/h1>\n<p> <\/p>\n<p>M \u8868\u793a source \u7684\u957f\u5ea6 N \u8868\u793a target \u7684\u957f\u5ea6 \u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(M+N) \u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(MN)<\/p>\n<pre><code>public class Solution {      \/**      * @param source:       * @param target:       * @return: return the index      *\/      public int strStr(String source, String target) {          \/\/\u83b7\u53d6\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6         int sourceLen = source.length();         int targetLen = target.length();          \/\/\u6ce8\u610f\u7279\u5224\uff0c\u82e5 target \u957f\u5ea6\u4e3a 0 \u5219\u7b54\u6848\u4e3a 0         if(targetLen == 0){             return 0;         }          \/\/\u82e5 target \u957f\u5ea6\u5927\u4e8e source\uff0c\u5219\u4e0d\u53ef\u80fd\u5339\u914d         if(targetLen &gt; sourceLen){             return -1;         }          for(int i = 0; i &lt; sourceLen-targetLen+1; i++){             \/\/k \u8868\u793a\u6bd4\u5bf9\u65f6 source \u4e2d\u6240\u6307\u5411\u7684\u5b57\u7b26             int k = i;             for(int j = 0; j &lt; targetLen; j++){                 if(source.charAt(k) == target.charAt(j)){                     \/\/\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u5339\u914d\u5b8c\u6210\uff0c\u8f93\u51fa\u7b54\u6848                     if(j == targetLen - 1){                         return i;                     }                     k++;                 }                 \/\/\u5176\u4e2d\u4e00\u4e2a\u5b57\u7b26\u65e0\u6cd5\u5339\u914d\uff0c\u76f4\u63a5\u9000\u51fa\u505a\u4e0b\u4e00\u6b21\u5faa\u73af                 else{                     break;                 }             }         }         return -1;     } } <\/code><\/pre>\n<p>\u66f4\u591a\u9898\u89e3\u53c2\u89c1<\/p>\n<h1>\u8bc4\u8bba\u533a\u53ef\u9886\u53d6LintCode 7 \u5929 VIP<\/h1>\n<p>  <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3087415\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             9C06EB                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3087416\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jtwor <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9\u53ef\u4ee5\u3002\u3002replace \u5982\u679c\u957f\u5ea6\u53d8\u5316\u4e86\u5c31\u662f\u5b58\u5728 \u5982\u679c\u5b58\u5728\u5c31 split\uff0c\u53d6\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u7684\u957f\u5ea6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3087417\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : easonHHH <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jtwor #2 \u7b97\u6cd5\u9898\u7528 replace&#8230;\u6211\u611f\u89c9\u5c31\u662f\u4f5c\u5f0a <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/09\/504J5BO2-39.png\" alt=\"\u5de8\u786c\u5bb6\u9762\u8bd5\u9898\uff1a\u5b57\u7b26\u4e32\u67e5\u627e\uff08\u5e26\u89e3\u9898\u601d\u8def\uff09\"> <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3087418\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jtwor <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @easonHHH \u6ed1\u7a3d) \u6392\u5e8f\u4e5f\u662f \u5904\u7406\u5b57\u7b26\u4e32 \u5237\u9898\u6211\u7ecf\u5e38 re \u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5de8\u786c\u5bb6\u9762\u8bd5\u9898\uff1a\u5b57\u7b26\u4e32\u67e5\u627e\uff08\u5e26\u89e3\u9898\u601d&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\/152202"}],"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=152202"}],"version-history":[{"count":1,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/152202\/revisions"}],"predecessor-version":[{"id":152204,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/152202\/revisions\/152204"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=152202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=152202"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=152202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}