24 - Implement strStr()

本文介绍了一种高效的字符串搜索算法——Boyer-Moore算法,并提供了详细的实现过程。通过对传统逐个比较方法的改进,该算法能够在遍历过程中减少不必要的比较,显著提升搜索速度。

Implement strStr().

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


solution: 方法一,最直观和最没效率的自然是一遍又一遍的遍历比对;

方法二,在方法一的实现过程中,我们能遇到这些情况,遍历的过的字符串会一再的遍历,然而很多情况是可以剪除,直接跳到合适的位置上的,所以,这里我们想到了两个引申的算法,kmp和boyer-moore算法,这里我仅实现了boyer-moore算法,该算法的原理就是在遍历时同时记录计算下一个跳转的位置,根据的原理是下一个字符在比对字符串中出现的位置,参看:

wiki: http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm

一个blog:http://www.ruanyifeng.com/blog/2013/05/boyer-moore_string_search_algorithm.html,这篇文章里详细介绍了该算法,然后引用了算法创始人的一个demo:http://www.cs.utexas.edu/~moore/best-ideas/string-searching/fstrpos-example.html,跟着跑一遍很有帮助。

方法三,看了leetcode介绍的该题tutor:http://leetcode.com/2010/10/implement-strstr-to-find-substring-in.html, 还有其它算法,当然它这里介绍的可能还是不够多,不过对于我来说只了解了里面三个算法就差不多了。


这里实现了Boyer-moore算法,但是没加判断后缀,以后再做

class Solution {
public:
    int locateCh(char *needle, char target, int length)
    {
        int offset = length - 1;
        while( needle[offset] != target )
        {
            if( offset <= 0 )
                return -1;
            offset --;
        }
        return offset;
    }

    char *strStr(char *haystack, char *needle) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int len = 0;
        int index = 0;
        
        if(needle == NULL || haystack == NULL)
            return NULL;
        else if(*needle == '\0')
            return haystack;
        
        while(needle[len] != '\0')
        {
            if(haystack[ index ] == '\0')
                return NULL;
            len++;
            index++;
        }
        
        int longlen = 0;
        while( haystack[longlen] != '\0' )
        {
            longlen++;
        }
        
        index -= 1;
        len -= 1;
        
        while( index < longlen )
        {
            int bad = len;
            int in = index;
            while( haystack[in] == needle[bad] )
            {
                if(bad <= 0)
                    return haystack+in;
                bad --;
                in --;
            }
            
            int offset = locateCh(needle, haystack[in], bad);
            int move = bad - offset;
            
            index += move;
        }
        return NULL;
    }
};




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值