首页 > 代码库 > 字符串模式匹配算法之二:KMP算法

字符串模式匹配算法之二:KMP算法

KMP算法简介

KMP算法全称叫做Knuth-Morris-Pratt Algorithm。

被搜索的字符串称为主串,待搜索的字符串称为模式串

我们知道朴素模式匹配算法:http://blog.csdn.net/chfe007/article/details/43448655是很低效的,KMP算法从模式串出发,发现模式串中隐藏的信息来减少比较的次数,具体如何做到的可以移步这个链接:http://kb.cnblogs.com/page/176818/

KMP算法的关键在于next数组值的推导

next数组推导

    void getNext(char *needle, int len, int *next) {
        int i = 0, j = -1;
        next[0] = -1;
        while (i < len - 1) {
            if (j == -1 || needle[i] == needle[j]) next[++i] = ++j;
            else j = next[j];
        }
    }

next数组推导的改进

    void getNextImp(char *needle, int len, int *next) {
        int i = 0, j = -1;
        next[0] = -1;
        while (i < len - 1) {
            if (j == -1 || needle[i] == needle[j]) {
                ++i;
                ++j;
                next[i] = (needle[i] == needle[j] ? next[j] : j);
            }
            else
                j = next[j];
        }
    }

KMP算法实现

    int strStr(char *haystack, char *needle) {
        int haystackLen = strlen(haystack);
        int needleLen = strlen(needle);
        int *next = new int[needleLen];
        // getNext(needle, needleLen, next);
        getNextImp(needle, needleLen, next);

        int i = 0, j = 0;
        while (i < haystackLen && j < needleLen) {
            if (j == -1 || haystack[i] == needle[j]) {
                ++i;
                ++j;
            }
            else {
                j = next[j];
            }
        }

        return j >= needleLen ? i - needleLen : -1;
    }

字符串模式匹配算法之二:KMP算法