您的位置:首页 > 其它

kmp字符串匹配C实现

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

以下是代码实现

#include
#include
#include
#include
#include

//分析模式串,计算出每个字符的最大前缀和后缀字符匹配数,得到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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: