• 字符串匹配算法BF和KMP总结


    背景

    来看一道leetcode题目:
    Implement strStr().

    Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

    让我们找到第一个子串的位置,这就是典型的字符串匹配问题。首先想到的就是暴力求解,时间复杂度O(mn).

    BF算法实现

    class Solution {
    public:
        int strStr(string haystack, string needle) {
            if (haystack.size() < needle.size())
                return -1;
            for(int i = 0; i <= haystack.size() - needle.size(); i++) {
                int flag = 1;
                for (int j = 0; j < needle.size(); j++) {
                    if (haystack[i + j] != needle[j]) {
                        flag = 0;
                        break;
                    }
                }
                if (flag == 1) {
                    return i;
                }
            }
            return -1;
        }
    };
    

    但是我们可以发现,其实有些字符串我们已经匹配过了,就不需要再次匹配了,这就是经典的KMP算法,时间复杂度为O(m + n)。KMP算法的讲的比较好的,可以参考这个博客:http://www.cnblogs.com/c-cloud/p/3224788.html

    KMP算法实现

    class Solution {
    public:
        int strStr(string haystack, string needle) {
            if (haystack.size() == 0) {
                if (needle.size() == 0)
                    return 0;
                else
                    return -1;
            } else if (needle.size() == 0)
                return 0;
            if (haystack.size() < needle.size())
                return -1;
    
            vector<int> Next(needle.size());
            getNext(needle, Next);
    
            for (int i = 0; i < haystack.size(); ) {
            	// 如果剩下的字符串比目标字符串更短,那么直接返回-1.
                if (haystack.size() - i < needle.size())
                    return -1;
                int num = 0;
                int step = 1;
                for (int j = 0; j < needle.size(); j++) {
                    if (haystack[i + j] == needle[j]) {
                        num++;
                    } else
                        break;
                }
                if (num == needle.size())
                    return i;
                if (num)
                    step = num - Next[num - 1];
                i = i + step;
            }
            return -1;
        }
        void getNext(string needle, vector<int>& next) {
            int k = 0;
            next[0] = 0;
            for (int i = 1; i < needle.size(); i++) {
                while (k > 0 && needle[i] != needle[k])
                    k = next[k - 1];
                if (needle[i] == needle[k])
                    k++;
                next[i] = k;
            }
        }
    };
    
  • 相关阅读:
    Spring通过springframework.data的@PageableDefault注解分页
    Specifications动态查询
    IDEA生成序列号serialVersionUID快捷键
    JS组件系列——表格组件神器:bootstrap table(还没细看)
    南昌网络赛 Distance on the tree(可持久化线段树)
    南昌网络赛 Max answer(单调栈)
    HDU 6148 Valley Numer(数位DP)
    python 二位dict
    Codeforces Round #230 (Div. 2) 题解
    Codeforces Round #228 (Div. 2) 题解
  • 原文地址:https://www.cnblogs.com/zhonghuasong/p/7088703.html
Copyright © 2020-2023  润新知