在大字符串寻找共出现几次子串的位置(KMP算法C写法)

int matchCount(char* sourceString, char* subString)
{
	int subCnt[100]; subCnt[0] = 0;
	unsigned int len1 = strlen(sourceString);
	unsigned int len2 = strlen(subString);


	for (int i=1, int j=0; i < len2; i++)
	{	
		while(j>0 && subString[j] != subString[i])
		{
			j=subCnt[j-1];
		}
		if (subString[i] == subString[j])
		{
			j++;
		}
		subCnt[i]=j;
	}
	int count =0;
	for (i=0, j=0; i<len1; i++)
	{
		while(j>0 && subString[j] != sourceString[i])
		{
			j = subCnt[j -1];
		}
		if (subString[j] == sourceString[i])
		{
			j++;
			if (len2 == j)
			{
				count++;
			}
		}
	}
	return count;
}

    原文作者:KMP算法
    原文地址: https://blog.csdn.net/zhuxincheng_1218/article/details/8617458
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞