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. etymology-F

    forsake [fə'seɪk] vt.放弃:断念. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400 ...

  2. Microsoft Team Foundation Server 2010 安装 序列号 注册码(转载)

    安装过程: 一.安装操作系统 安装Windows 2008 R2简体中文版 二.准备安装过程中的需要的用户账户,并设置相应权限. 具体流程如下: 1.点击“开始”——“管理工具”——“计算机管理” 2 ...

  3. php get_called_class()函数与get_class函数的区别

    get_class (): 获取当前调用方法的类名: get_called_class():获取静态绑定后的类名: 有例为证: class Foo{ public function test(){ v ...

  4. JUC之AQS

    AbstractQueuedSynchronizer(AQS) AQS是并发容器里的同步器,从jdk1.5开始引入了并发包,java.util.concurrent,提供了一个基于first in f ...

  5. 用jquery制作一个简单的导航栏

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. SQL 数据库分页语句

    declare @pagesize integer,@cpage integer; ; ; SELECT TOP (@pagesize) * FROM (SELECT row_number() ove ...

  7. 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换(转)

    转自: http://blog.csdn.net/jenghau/article/details/5532265 文件指针/句柄(FILE*).文件描述符(fd)以及 文件路径(filepath)的相 ...

  8. ZThread在Windows下打印若干DEBUG信息到console的原因

    代码见这篇随笔 在Windows下的运行结果:ZThread打印了一堆东西(并不是我写的) 文件结构: 最开始我以为是编译选项没弄对,同样的代码放到Linux下编译,还是打印这些信息 注意我在Linu ...

  9. onActivityResult 通过case对不同情况进行处理

    @Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) { switch (result ...

  10. 设置MATLAB中Current Folder的默认文件夹(转载)

    设置MATLAB中Current Folder的默认文件夹 在我们使用MATLAB的过程中,其Current Folder面板会给我们带来一定的便利性.但遗憾的是,MATLAB自身没有提供友好的设置界 ...