参考:http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html
自己写的很简单的KMP字符串匹配
 int KMP(string s,string p){
int pLen = p.length();
int* next = new int[pLen];
next[] = -; int k = -;
int j = ;
while(j < pLen - ){
if(k == - || p[k] == p[j]){
k++;
j++;
next[j] = k;
}else{
k = next[k];
}
} //print next array;
for(int k=;k<pLen;k++){
std::cout << next[k]<< ",";
}
std::cout << std::endl; j = ;
size_t i = ;
size_t sLen = s.length();
while(i<sLen && j < pLen){
if(j == - || s[i] == p[j]){
i++;
j++;
}else{
j = next[j];
}
} delete next;
if(j == pLen){
return i - j;
}else{
return -;
}
}

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

 class Solution {
public:
char *strStr(char *haystack, char *needle) {
if(haystack == nullptr || needle == nullptr) return nullptr; int i = ,j =;
int start = ;
while(haystack[i] != '\0' && needle[j] != '\0'){
if(haystack[i] == needle[j]){
i++;
j++;
}else{
start++;
i = start;
j = ;
}
}
if(needle[j] == '\0') return haystack+start;
else return nullptr;
}
};

面试题30:KMP 字符串查找的更多相关文章

  1. KMP字符串查找算法

    #include <iostream> #include <windows.h> using namespace std; void get_next(char *str,in ...

  2. 回朔法/KMP算法-查找字符串

    回朔法:在字符串查找的时候最容易想到的是暴力查找,也就是回朔法.其思路是将要寻找的串的每个字符取出,然后按顺序在源串中查找,如果找到则返回true,否则源串索引向后移动一位,再重复查找,直到找到返回t ...

  3. 数据结构与算法--KMP算法查找子字符串

    数据结构与算法--KMP算法查找子字符串 部分内容和图片来自这三篇文章: 这篇文章.这篇文章.还有这篇他们写得非常棒.结合他们的解释和自己的理解,完成了本文. 上一节介绍了暴力法查找子字符串,同时也发 ...

  4. 字符串类——KMP子串查找算法

    1, 如何在目标字符串 s 中,查找是否存在子串 p(本文代码已集成到字符串类——字符串类的创建(上)中,这里讲述KMP实现原理) ? 1,朴素算法: 2,朴素解法的问题: 1,问题:有时候右移一位是 ...

  5. KMP 算法 & 字符串查找算法

    KMP算法 Knuth–Morris–Pratt algorithm 克努斯-莫里斯-普拉特 算法 algorithm kmp_search: input: an array of character ...

  6. Rabin-Karp指纹字符串查找算法

    首先计算模式字符串的散列函数, 如果找到一个和模式字符串散列值相同的子字符串, 那么继续验证两者是否匹配. 这个过程等价于将模式保存在一个散列表中, 然后在文本中的所有子字符串查找. 但不需要为散列表 ...

  7. Sunday算法(字符串查找、匹配)

    字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).两个算法在最坏情况下均具有线性的查找时间.但是在实用上,KMP算法并不比最简单的 ...

  8. lintcode:strStr 字符串查找

    题目: 字符串查找 字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数.你的任务是实现这个函数. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source ...

  9. KMP字符串模式匹配详解(转)

    来自CSDN     A_B_C_ABC 网友 KMP字符串模式匹配通俗点说就是一种在一个字符串中定位另一个串的高效算法.简单匹配算法的时间复杂度为O(m*n);KMP匹配算法.可以证明它的时间复杂度 ...

随机推荐

  1. hdu 4995 离线处理+模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=4995 给定一维坐标下的n个点,以及每个点的权值,有m次查询,每次将查询的x点上的权值修改为离x最近的k个点权值的 ...

  2. Android-AndroidStudio莫名其妙的错误-finished with non-zero exit value 1

    上一篇博客,Android-AndroidStudio莫名其妙的错误-finished with non-zero exit value 1,解决了由于 string.xml 字符导致的: 而这篇博客 ...

  3. EasyUI 让dialog中的treegrid的列头固定

    先上效果: 最主要是在treegrid要加上"fit:true "如果不加那么就会用diglog的滚动条,导致treegrid的头就没办法固定. Code<div id=&q ...

  4. (原创)PetaPoco使用小记(2014-5-5更新)

    接触PetaPoco已经有一段时间了,为了全面了解一下PetaPoco,刚好结合目前在做的一个项目,对常用的几个业务操作用PetaPoco进行改写,如增删改查.分页以及存储过程的调用,在文章的最后附上 ...

  5. sqlserver错误2,error 40

    打开配置管理器:开始-> sqlserver2014->配置工具->配置管理器 选择sqlserver服务,并将右侧箭头的指向右击设为启动就OK了

  6. Geometry-587. Erect the Fence

    There are some trees, where each tree is represented by (x,y) coordinate in a two-dimensional garden ...

  7. git install

    wget -O git-master.zip https://codeload.github.com/git/git/zip/master unzip git-master.zip cd git-ma ...

  8. 读取GY-951模块数据(Linux)

    参考:https://github.com/Razor-AHRS/razor-9dof-ahrs/wiki/Tutorial#sensor-calibration 链接中使用的硬件是SparkFun“ ...

  9. poj2479 Maximum sum

    http://poj.org/problem?id=2479 题目大意:给定一组n个整数:a ={a1, a2,…,我们定义一个函数d(a)如下: 你的任务是计算d(A).输入由T(<=30)测 ...

  10. Swift 里字符串(三)small String

     small string, 只有两个 UInt64 的字,这里面存储了所有的信息. 内存布局如下:  第二个 UInt64 存储了标记位和长度信息,以及部分字符串的值 // Get an int ...