kmp字符串匹配C实现

关于kmp字符串匹配的算法,可以在网上google,就不在此赘述。推荐这篇文章:http://blog.csdn.net/v_july_v/article/details/7041827

以下是代码实现

#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>

//分析模式串,计算出每个字符的最大前缀和后缀字符匹配数,得到next数组
int analyzeMatchString(const char *pMatchString, int **ppMatchCntArray, int *pCount)
{
    size_t len;
    bool bMatch;
    int *pNextArray;

    assert(NULL != pMatchString && NULL != ppMatchCntArray && NULL != pCount);

    if ('\0' != *pMatchString)
    {
        len = strlen(pMatchString);
        pNextArray = (int *)malloc((len+1)*sizeof(int));
        if (NULL != pNextArray)
        {
            pNextArray[0] = -1;
            pNextArray[1] = 0;
            //遍历所有的模式串字符
            for (size_t i = 1; i < len; i++)
            {
                pNextArray[i+1] = 0;
                //匹配0到i子串的最大前缀和后缀匹配字符数
                for (size_t j = i; j > 0; j--)
                {
                    //从最大的前缀和后缀子串进行匹配
                    bMatch = true;
                    for (size_t k = 0; k < j; k++)
                    {
                        if (pMatchString[k] != pMatchString[i-j+k+1])
                        {
                            bMatch = false;
                            break;
                        }
                    }
                    if (bMatch)//若当前前缀和后缀匹配,则直接跳出循环
                    {
                        pNextArray[i+1] = j;
                        break;
                    }
                }
            }
            *ppMatchCntArray = pNextArray;
            *pCount = len + 1;
            return 0;
        }
        return ENOMEM;
    }
    return EINVAL;
}

//kmp算法匹配串
const char *strstr_kmp(const char *string, const char *matchString, int *pError = NULL)
{
    int ret;
    int matchedCnt;
    int count;
    int *pMatchCntArray;
    const char *tmpStr;

    assert(NULL != string && NULL != matchString);

    if ('\0' == *matchString)
    {
        return string;
    }
    ret = analyzeMatchString(matchString, &pMatchCntArray, &count);
    if (0 == ret)
    {
        assert(NULL != pMatchCntArray && count > 0);
        if (NULL != pError)
        {
            *pError = 0;
        }

        matchedCnt = 0;
        while ('\0' != *string)
        {
            tmpStr = string;
            while ('\0' != *tmpStr)
            {
                if (*tmpStr == matchString[matchedCnt])
                {
                    if (matchedCnt == count - 2)//匹配成功
                    {
                        free(pMatchCntArray);
                        return string;
                    }
                    matchedCnt++;
                    tmpStr++;
                }
                else
                {
                    //向后移动的位数=已匹配字符数-上一个字符的最大匹配数
                    string += matchedCnt - pMatchCntArray[matchedCnt];
                    matchedCnt = 0;
                    break;
                }
            }
        }
        free(pMatchCntArray);
    }
    else if (NULL != pError)
    {
        *pError = ret;
    }
    return NULL;
}

int main(int argc, char **argv)
{
    const char *substr;

    substr = strstr_kmp("BBC ABCDAB ABCDABCDABDE", "B ABCDAB ");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值