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. hbase 批量插入api

    1.数据格式a.txt: 1363157985066 13726230503 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 24 27 ...

  2. OpenSSL生成证书详解 如何使用OpenSSL生成自签证书 转载

    原文:http://my.oschina.net/fajar/blog/425478 使用OpenSSL生成自签证书(亲测) 一,前言 读过我博客的小伙伴儿都知道,我一般在前言里面会提到为什么写这篇博 ...

  3. unity, trail renderer gone black on iOS

    给物体加了个trail renderer,使用了Legacy Shaders/Transparent/Diffuse,并将颜色调成白色半透明.在编辑器里效果是对的,但在ios上真机测试变成黑色的.然后 ...

  4. 跨Server查询

    GO RECONFIGURE GO GO RECONFIGURE GO SELECT MAX(OldestCall) FROM ( SELECT FeildAA As FeildAAFROM OPEN ...

  5. [na] centos如何通过vmware Windows共享文件

    参考 自我感觉都会使用Windows中的文件.在Windows与linux之间互传文件是一个问题.本方法介绍的是在linux下挂载Windows共享文件夹的方法来实现的 首先安装VMware Tool ...

  6. Unix删除当前目录可执行文件

    On GNU versions of find you can use -executable: find . -type f -executable -printFor BSD versions o ...

  7. 基于flink快速开发实时TopN程序

    TopN 是统计报表和大屏非常常见的功能,主要用来实时计算排行榜.流式的TopN可以使业务方在内存中按照某个统计指标(如出现次数)计算排名并快速出发出更新后的排行榜. 我们以统计词频为例展示一下如何快 ...

  8. CSU 1329: 一行盒子

    1329: 一行盒子 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 740  Solved: 145[Submit][Status][Web Board ...

  9. unity shader(二)

  10. iOS swift 启动页加载广告(图片广告+视频广告)

    一般app在启动的时候都会有广告页,广告页用来加载自己的或者第三方的广告,广告的展示形式也多种多样,最近在看swift相关的东西,这里将提供支持加载图片广告和视频广告的解决方案 思路: 我们知道在加载 ...