70. Implement strStr() 与 KMP算法

Implement strStr()

Implement strStr().

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

MY: Question.

思路: 逐步查找。当出现不同时,如何回溯是关键。

Solution A:

class Solution {
public:
    char *strStr(char *haystack, char *needle) {
        int i = 0, j = 0;
        while(haystack[i] != ‘\0‘ && needle[j] != ‘\0‘) {
            if(haystack[i] == needle[j])
                ++i, ++j;
            else
                i = i-j+1, j = 0;
        }
        return needle[j] == ‘\0‘ ? haystack+(i-j) : NULL;
    }
};

Solution B 与经典 KMP 算法:

对模式串 P 设置回溯数组 next. (next 只有模式串 P 的特性有关系,与目标串没有关系。)

next 的求法:

next[0] = 0; (0 位置最后匹配,下次还从此位置开始匹配(舍去从-1开始,没有意义))

next[pos] = (P[next[pos-1]] == P[pos] ? next[pos-1]+1 : 0);

(若回溯后的字符与当前字符相同,则应设置回溯位置在前回溯位置之后。否则,设置回溯位置为0)

模式串中:

当前位置 pos 不能匹配时, 回溯到 next[pos-1] 重新开始匹配。

当前位置匹配,则继续下去。

#include <iostream>
#include <vector>
using namespace std; 

void getNext(char *P, vector<int> &next) {
	for (int i = 0; P[i] != ‘\0‘; ++i) {
		if (i == 0) next.push_back(0);
		else next.push_back((P[i] == P[next[i-1]]) ? next[i-1]+1 : 0);
	}
}
class Solution {
public:
	char *strStr(char *haystack, char *needle) {
		vector<int> next;
		getNext(needle, next);
		int i = 0, j = 0;
		while (haystack[i] != ‘\0‘ && needle[j] != ‘\0‘) {
			if (haystack[i] == needle[j]) ++i, ++j;
			else if (j == 0) ++i;
			else j = next[j-1];
		}
		return needle[j] == ‘\0‘ ? haystack+(i-j) : NULL;
	}
};

int main() {
	char *T = "missiissippi", *P = "issip";
	cout << (Solution().strStr(T, P) ? Solution().strStr(T, P) : "NULL") << endl;
	return 0;
}
时间: 2024-08-03 16:03:29

70. Implement strStr() 与 KMP算法的相关文章

Implement strStr()&amp;BF&amp;KMP

Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 思路:时间复杂度O(m*n),也就是BF(Brute Force)算法: code: class Solution { public: char *strStr(char *haystack, char *needle) { if(!*need

leetcode_28题——Implement strStr()(采用KMP算法,还没AC,但自己这边测试无误)

Implement strStr() Total Accepted: 49294 Total Submissions: 223057My Submissions Question Solution Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02):The s

Implement strStr() &amp;&amp; kmp算法

用这题复习下kmp算法.kmp网上坑爹的是有很多种匹配方式,容易混淆,后人要警惕啊.这里对要查找的字符串的next全部置为-1,预处理时i和next[i-1]+1相比较.和http://kb.cnblogs.com/page/176818/ 这里相似.预处理完再匹配,第i个字符不匹配则比较第next[i-1]+1个. class Solution{ public: char *strStr(char *haystack, char *needle){ if(haystack == NULL ||

Linux GCC下strstr的实现以及一个简单的Kmp算法的接口

今天做了一道题,要用判断一个字符串是否是另一个字符串的子串,于是查了一下strstr的实现. 代码如下: 1 char *strstr(const char*s1,const char*s2) 2 { 3 const char*p=s1; 4 const size_t len=strlen(s2); 5 for(;(p=strchr(p,*s2))!=0;p++) 6 { 7 if(strncmp(p,s2,len)==0) 8 return (char*)p; 9 } 10 return(0)

[字符串匹配、KMP]Implement strStr()

一.题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 二.分析 很容易想出O(M*N)的算法,也很容易实现.原串S,模式串P,利用Python中[i:j]方法, 在原串中匹配长度为len(p)的元素.若匹配,返回头:否则向右移.这样复杂度是O(M*N) 最快的是KMP,是O(M+N),看了一

用KMP算法实现strStr()

strStr()函数的用途是在一个字符串S中寻找某个字串P第一次出现的位置,并返回其下标,找不到时返回-1.最简单的办法就是找出S所有的子串和P进行比较,然而这个方法比较低效.假设我们从S的下标0和P的下标0开始对每个字符进行比较,如果相等则下标增加,比较后面的字符.如果两者一直相等直到P的下标达到最大值,则表示在S中找到了P,并且第一次出现的位置为0,返回0,但如果在中间某个位置两个字符不相等时,这时S的下标要退回到1,P的下标回到0,重新开始比较.后来,有三个牛觉得这样不爽,于是他们搞了一个

28. Implement strStr()(KMP字符串匹配算法)

Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa",

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

KMP算法小结

今天又把KMP算法看了一遍,特此小结. 扯淡的话: KMP算法主要用来模式匹配.正如Implement strStr() 中形容的一样,“大海捞针”,当时看到题中变量如此命名,真的感觉实在是再贴切不过了. 在介绍KMP算法之前,先介绍一下BF算法,叫的这么暧昧(who is GF?),其实就是最low的暴力算法.这个男票略暴力. 事实上,JDK1.7中String的contains的源码用的就是BF算法.(1.7中调用了indexOf,我记得1.6中是直接写的contains接口来着) 截取了一