KMP is a classic and yet notoriously hard-to-understand algorithm. However, I think the following two links give nice explanations. You may refer to them.

  1. KMP on jBoxer's blog;
  2. KMP on geeksforgeeks, with a well-commented C code.

I am sorry that I am still inable to give a personal explanation of the algorithm. I only read it from the two links above and mimic the code in the second link.

You may use this code to solve the problem Implement strStr() on LeetCode. My accepted 4ms C++ code using KMP is as follows.

 class Solution {
public:
int strStr(string haystack, string needle) {
int m = needle.length(), n = haystack.length();
if (!m) return ;
vector<int> lps = kmpProcess(needle);
for (int i = , j = ; i < n; ) {
if (needle[j] == haystack[i]) {
i++;
j++;
}
if (j == m) return i - j;
if (i < n && needle[j] != haystack[i]) {
if (j) j = lps[j - ];
else i++;
}
}
return -;
}
private:
vector<int> kmpProcess(string needle) {
int m = needle.length();
vector<int> lps(m, );
for (int i = , len = ; i < m; ) {
if (needle[i] == needle[len])
lps[i++] = ++len;
else if (len) len = lps[len - ];
else lps[i++] = ;
}
return lps;
}
};

[Algorithms] KMP的更多相关文章

  1. KMP笔记√//找最大子串,前缀自匹配长度

    假设s1里找s2,然后s2进去匹配假设在第三位失配那么说明前两位是匹配成功的 如果这时候将s2后移一位相当于将s2的第一位和s2的第二位比较,如果我们已知s1(1)≠s1(2)那么就可以直接后移两位 ...

  2. 程序员的算法课(11)-KMP算法

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...

  3. [ Python ] KMP Algorithms

    def pmt(s): """ :param s: the string to get its partial match table :return: partial ...

  4. 经典KMP算法C++与Java实现代码

    前言: KMP算法是一种字符串匹配算法,由Knuth,Morris和Pratt同时发现(简称KMP算法).KMP算法的关键是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的.比 ...

  5. kmp算法详解

    转自:http://blog.csdn.net/ddupd/article/details/19899263 KMP算法详解 KMP算法简介: KMP算法是一种高效的字符串匹配算法,关于字符串匹配最简 ...

  6. kmp 和boyer-moore

    <html> <head> <meta http-equiv="content-type" content="text/html; char ...

  7. kmp java implement--转

    http://cs.nyu.edu/~yap/classes/basic/progs/patternMatching/KMP.java /** * @file KMP.java * @synopsis ...

  8. Algorithms & Data structures in C++& GO ( Lock Free Queue)

    https://github.com/xtaci/algorithms //已实现 ( Implemented ): Array shuffle https://github.com/xtaci/al ...

  9. 一篇别人写的Kmp算法的讲解,多看多得

    kmp算法的理解与实现 博客分类: algorithms 算法      KMP算法曾被我戏称为看毛片算法,当时笑喷......大三那个时候硬着头皮把算法导论的kmp算法啃完,弄懂了kmp算法 的原理 ...

随机推荐

  1. 哈希key个数

    $length = keys %hashname; 则$length中得到的直接是该hash的key的个数.

  2. 将object格式转为json格式

    在页面内容显示时,有时需要用到json格式.但数据库内容的显示,需要将数据库中获取的格式转为json: using Newtonsoft.Json;public static string ToJso ...

  3. VSCode开发工具下载

    VSCode集成多语言和插件,方便开发和代码管理. 请到此处下载:https://code.visualstudio.com/Download

  4. memcache操作实例

    实例一: <?php //使用memcache类来操作 $mm = new Memcache(); $mm->addServer("192.168.70.114",11 ...

  5. Java并发编程(三):并发模拟(工具和Java代码介绍)

    并发模拟工具介绍 ① Postman : Http请求模拟工具 从图上我们可以看出,Postman模拟并发其实是分两步进行操作的.第一步:左边的窗口,在窗口中设置相关接口以及参数,点击运行进行第二步. ...

  6. Jetty - Handler源码分析

    1. 描述 基于Jetty-9.4.8.v20171121. Handler是Jetty服务处理器,用户Server处理HTTP请求. Handler可以做如下处理: (1)完全生成HTTP响应: ( ...

  7. C#生日提醒小工具

    一个很粗糙的版本,就当一个小例子看一下吧, 运行效果如下: 开发环境VS2017,用的WinForm,涉及一点xml,直接上图. 一.项目涉及的文件如下图: 二.每个文件内容: 1.MainForm  ...

  8. TomEE

    https://tomee.apache.org/ Apache TomEE, pronounced "Tommy", is an all-Apache Java EE 6 Web ...

  9. 数据库设计(四)数据库的规范化(Normalization)

    数据库的规范化 Database Normalization is a technique of organizing the data in the database. Normalization ...

  10. 可执行文件格式elf和bin

    区别 常用的可执行文件包含两类:原始二进制文件(bin)和可加载执行的二进制文件,在linux中可加载执行的二进制文件为elf文件. BIN文件是直接的二进制文件,内部没有地址标记.bin文件内部数据 ...