Implement strStr() && 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 || needle == NULL)
                return NULL;
            int sz = strlen(needle);
            vector<int>next(sz,-1);
            for(int i = 1; i < sz; i++){
                int index = i - 1;
                while(index != -1 && needle[next[index]+1] != needle[i])
                    index = next[index];
                next[i] = index==-1?-1:next[index]+1;
            }
            int i = 0;
            while(*haystack && i <sz){
                while(i != -1 && *haystack != needle[i])
                    i = i==0?-1:next[i-1]+1;
                 haystack++;
                 i++;
            }
            return i < sz?NULL:haystack-sz;
        }
};

/*                    a b C b C a d
                    -1 -1 -1 -1 -1 0 -1
                    abcab cabdabba  
                    a   b  c  a  b  d
                    -1 -1  -1 0  1  -1*/
时间: 2024-08-07 08:19:33

Implement strStr() && kmp算法的相关文章

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_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

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

[字符串匹配、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),看了一

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

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算法实现strStr()

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

【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接口来着) 截取了一