Implement strStr()&BF&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(!*needle)
            return haystack;

        char *p,*q;
        char *p_advance=haystack;

        for(q=&needle[1];*q;++q)
            ++p_advance;

        for(p=haystack;*p_advance;++p_advance)
        {
            char *p_old=p;
            q=needle;
            while(*p&&*q&&*p==*q)
            {
                ++p;
                ++q;
            }
            if(!*q)
                return p_old;
            p=p_old+1;
        }
        return NULL;
    }
};

此问题还有更经典的KMP算法;

code:

class Solution {
public:
    char *strStr(char *haystack, char *needle) {
        if(!*needle)
            return haystack;

        int *next=new int[strlen(needle)];
        getNext(needle,next);

        for(int i=0,j=0;i<strlen(haystack);)
        {
            if(j==-1||haystack[i]==needle[j])
            {
                ++i;
                ++j;
            }
            else
                j=next[j];

            if(j==strlen(needle))
                return (char*)(i-strlen(needle)+haystack);
        }
        return NULL;
    }

    //array of next
    //as for char *s="ababa",the corresponding array is:-1,0,0,1,2
    void getNext(char *needle,int next[])
    {
        next[0]=-1;
        int k=-1;
        int j=0;
        while(j<strlen(needle)-1)
        {
            if(k==-1||needle[j]==needle[k])
            {
                ++j;
                ++k;
                next[j]=k;
            }
            else
                k=next[k];
        }
    }
};

KMP算法参考:

这篇文章对KMP原理有着清晰点的说明;

算法的实现可以参考这篇博文;

时间: 2024-11-05 18:51:10

Implement strStr()&BF&KMP的相关文章

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),看了一

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

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() 找到字串返回位置。

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 翻译: 找到字符串的子串位置,并返回.如果没有则返回-1 思路: 通过最简单的BF遍历,如果不符合则指向下一个字符,最后如果长度等于子串长度,则返回位置. 代码: public static int strStr(String haysta

[LeetCode] Implement strStr() [18]

题目 Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 原题链接(点我) 解题思路 字符串匹配这也是个老题了,方法主要有下面4种, 1. 暴利破解法(BF),这个没啥说的,就是一轮一轮的比较,知道遇到相匹配的,这个的时间复杂度为O(n^2). 2. KMP,这应该是字符串匹配领域中最长听说的算

LeetCode(28)Implement strStr()

题目 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 signature of the function had been updated to return the index instead of the pointer. If you st

【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

LeetCode Implement strStr()

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 signature of the function had been updated to return the index instead of the pointer. If you still