[Algorithms] KMP
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.
- KMP on jBoxer's blog;
- 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的更多相关文章
- KMP笔记√//找最大子串,前缀自匹配长度
假设s1里找s2,然后s2进去匹配假设在第三位失配那么说明前两位是匹配成功的 如果这时候将s2后移一位相当于将s2的第一位和s2的第二位比较,如果我们已知s1(1)≠s1(2)那么就可以直接后移两位 ...
- 程序员的算法课(11)-KMP算法
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...
- [ Python ] KMP Algorithms
def pmt(s): """ :param s: the string to get its partial match table :return: partial ...
- 经典KMP算法C++与Java实现代码
前言: KMP算法是一种字符串匹配算法,由Knuth,Morris和Pratt同时发现(简称KMP算法).KMP算法的关键是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的.比 ...
- kmp算法详解
转自:http://blog.csdn.net/ddupd/article/details/19899263 KMP算法详解 KMP算法简介: KMP算法是一种高效的字符串匹配算法,关于字符串匹配最简 ...
- kmp 和boyer-moore
<html> <head> <meta http-equiv="content-type" content="text/html; char ...
- kmp java implement--转
http://cs.nyu.edu/~yap/classes/basic/progs/patternMatching/KMP.java /** * @file KMP.java * @synopsis ...
- Algorithms & Data structures in C++& GO ( Lock Free Queue)
https://github.com/xtaci/algorithms //已实现 ( Implemented ): Array shuffle https://github.com/xtaci/al ...
- 一篇别人写的Kmp算法的讲解,多看多得
kmp算法的理解与实现 博客分类: algorithms 算法 KMP算法曾被我戏称为看毛片算法,当时笑喷......大三那个时候硬着头皮把算法导论的kmp算法啃完,弄懂了kmp算法 的原理 ...
随机推荐
- ExtJS学习------Ext.define的继承extend,用javascript实现相似Ext的继承
(1)Ext.define的继承extend 详细实例: Ext.onReady(function(){ //Sup Class 父类 Ext.define('Person',{ config:{ n ...
- JUC组件扩展(二)-JAVA并行框架Fork/Join(三):在任务中抛出异常
在java当中,异常一共分为两种.一种是运行时异常,一种是非运行是异常. 非运行时异常:这些异常必须在方法上通过throws子句抛出.或者在方法体内进行try{…}catch{…}来捕获异常. 运行时 ...
- 为每个页面加上Session判断
首先新建一个类,继承自System.Web.UI.Page,然后重写OnInit,如下: using System; using System.Data; using System.Configura ...
- Atitit.加密算法 des aes 各个语言不同的原理与解决方案java php c#
Atitit.加密算法 des aes 各个语言不同的原理与解决方案java php c# 1. 加密算法的参数::算法/模式/填充 1 2. 标准加密api使用流程1 2.1. Md5——16bi ...
- DDR3调试总结
DDR3调试总结 本文为原创,转载请注明作者与出处 http://blog.csdn.net/hanfei_1/article/details/70546010 以前同是DDR3的无知少年,由于项目需 ...
- 选择如何的系统更能适合App软件开发人员?
手机这个词早已经同吃喝玩乐一样.成为了人们生活中的必备元素. 尤其是iPhone一炮走红之后,不但手机世界发生了巨大变化,整个科技产业似乎都格局性的改变.直至今日,手机市场的竞争更是日趋白炽化,这就给 ...
- JSP中out.write()和out.print()的区别
out对象的类型是JspWriter.JspWriter继承了java.io.Writer类. 1)print方法是子类JspWriter,write是Writer类中定义的方法: 2)重载的prin ...
- erlang supervisor simple_one_for_one实例
simple_one_for_one vs one_for_one: 相同点: 这种Restart Strategy和one_for_one基本相同(即当一个child process挂掉后,仅仅重启 ...
- wxpy学习
准备工作 安装 pip install -U wxpy -i "https://pypi.doubanio.com/simple/" 通过python脚本来发送消息给好友 from ...
- linux学习笔记6--命令mv
mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. mv命令用来对文件或目录重新命名,或者将文 ...