[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算法 的原理 ...
随机推荐
- js根据对象的某一属性进行排序
function compare(property) { return function (a, b) { var value1 = new Date(a[property]); var value2 ...
- MongoDB - Cursors
db.collection.find()查询集合会返回一个包含查到的文档的游标.在mongo shell中,如果没有定义一个变量来该游标的内容,默认会迭代返回20个文档. > db.users. ...
- python 脚本(获取指定文件夹、指定文件格式、的代码行数、注释行数)
1.代码的运行结果: 获取 指定文件夹下.指定文件格式 文件的: 总代码行数.总注释行数(需指定注释格式).总空行数: #coding: utf-8 import os, re # 代码所在目录 FI ...
- struts和spring整合
开发流程: 1)引jar包,可以在配置工程中设置用户libarary,然后直接引入.如果在web-inf/lib没有用户导入的lib文件,可以参考问题0的解决方案 需要的是struts_core,sp ...
- 智能指针tr1::shared_ptr、boost::shared_ptr使用
对于tr1::shared_ptr在安装vs同一时候会自带安装,可是版本号较低的不存在.而boost作为tr1的实现品,包括 "Algorithms Broken Compiler Work ...
- Atitit.提升软件Web应用程序 app性能的方法原理 h5 js java c# php python android .net
Atitit.提升软件Web应用程序 app性能的方法原理 h5 js java c# php python android .net 1. 提升单例有能力的1 2. 减少工作数量2 2.1. 减少距 ...
- 1.Vector(向量容器)
一.概述 Vectors 包含着一系列连续存储的元素,其行为和数组类似.访问Vector中的任意元素或从末尾添加元素都可以在常量级时间复杂度内完成,而查找特定值的元素所处的位置或是在Vector中插入 ...
- python-sqlite3之占位符
The sqlite3 module supports two kinds of placeholders: question marks (qmark style) and named placeh ...
- 5.14日学习内容1:jquery表单相关知识
<script> $comment.animate({height:'+=50'},400);//在原来的基础上加50: $('.smaller').click(function(){ i ...
- CCNA2.0笔记_VLSM
子网化:把一个大的主类网段,通过借位的方式逻辑划分多个子网段,应用于多个广播域: 做子网划分的时候,子网掩码最多只能到30位,不能再多划(因为至少要保留4个地址,即2个主机位) FLSM(定长子网掩码 ...