C++ KMP 算法

简介:

KMP算法是一种改进的字符串匹配算法,由D.E.Knuth与V.R.Pratt和J.H.Morris同时发现,因此人们称它为克努特——莫里斯——普拉特操作(简称KMP算法).

KMP算法的关键是根据给定的模式串W1,m,定义一个next函数,next函数包含了模式串本身局部匹配的信息.


#include <iostream>
#include <cstring>
#include <string>
#include <set>
#include <map>
using namespace std;

void BuildPatchMatchTable(int *partMatchTable, char *findstr)
{
    if(findstr == NULL)
        return;
    partMatchTable[0] = 0;
    int sizefind = strlen(findstr);
    for(int i = 1; i < sizefind; ++i)
    {
        set<string> preset;
        string tmppre = "";
        tmppre = findstr[0];
        preset.insert(tmppre);
        for(int j = 1; j < i; ++j)
        {
            tmppre = tmppre + findstr[j];
            preset.insert(tmppre);
        }

        set<string> postset;
        string tmppost = "";
        tmppost = findstr[i];
        postset.insert(tmppost);
        for(int j = i - 1; j > 0; --j)
        {
            tmppost =  findstr[j] + tmppost;
            postset.insert(tmppost);
        }
        set<string> comset;
        for(set<string>::iterator beg = preset.begin(); beg != preset.end(); ++beg)
        {
            if(postset.count(*beg) > 0)
                comset.insert(*beg);
        }
        int maxlen = 0;
        for(set<string>::iterator beg = comset.begin(); beg != comset.end(); ++beg)
        {
            if((*beg).size() > maxlen)
                maxlen = (*beg).size();
        }
        partMatchTable[i] = maxlen;
    }
}

int kmp(char *srcstr, char *findstr)
{
    if(srcstr == NULL || findstr == NULL)
        return -1;
    int lensrc = strlen(srcstr);
    int lenfind = strlen(findstr);
    int *partMatchTable = new int[lenfind];
    BuildPatchMatchTable(partMatchTable, findstr);
    for(int i = 0; i < lenfind; ++i)
        cout << findstr[i] << "\t" << partMatchTable[i] << endl;
    int curFind = 0;
    for(int i = 0; i < lensrc; )
    {
        if(findstr[curFind] == srcstr[i])
        {
            ++i;
            ++curFind;
        }
        else
        {
            if(curFind == 0)
                ++i;
            else
            {
                int movestep = curFind - partMatchTable[curFind-1];
                i += movestep;
                curFind = 0;
            }
        }
        if(curFind == lenfind)
        {
            delete []partMatchTable;
            return i - lenfind;
        }
    }
    return -1;
    delete []partMatchTable;
}
int main()
{
    char srcStr[] = "bbcabcdababcdabcdabde";
    char findStr[] = "abcdabd";
    cout << "pos:" << kmp(srcStr, findStr) << endl;


    char srcStr2[] = "substring searching algorithm search";
    char findStr2[] = "search";
    cout << "pos:" << kmp(srcStr2, findStr2) << endl;


}


目录
相关文章
|
27天前
|
机器学习/深度学习 安全 算法
【图论】【割点】【C++算法】928. 尽量减少恶意软件的传播 II
【图论】【割点】【C++算法】928. 尽量减少恶意软件的传播 II
|
13天前
|
存储 缓存 算法
C++从入门到精通:4.6性能优化——深入理解算法与内存优化
C++从入门到精通:4.6性能优化——深入理解算法与内存优化
|
13天前
|
存储 算法 程序员
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
|
22天前
|
存储 算法
图解Kmp算法——配图详解(超级详细)
图解Kmp算法——配图详解(超级详细)
|
27天前
|
人工智能 算法 BI
【图论】【 割边】【C++算法】1192. 查找集群内的关键连接
【图论】【 割边】【C++算法】1192. 查找集群内的关键连接
|
27天前
|
算法 测试技术 C#
【模拟】【C++算法】2826. 将三个组排序
【模拟】【C++算法】2826. 将三个组排序
|
27天前
|
算法 测试技术 C#
【数学】【C++算法】780. 到达终点
【数学】【C++算法】780. 到达终点
|
27天前
|
机器学习/深度学习 算法 测试技术
【深度优先】【图论】【C++算法】2045. 到达目的地的第二短时间
【深度优先】【图论】【C++算法】2045. 到达目的地的第二短时间
|
21小时前
|
C++
【C++】类与对象(日期计算器)
【C++】类与对象(日期计算器)
10 0
|
1天前
|
C语言 C++
【C++】string类(常用接口)
【C++】string类(常用接口)
8 1