求两个字符串的最大公共字符串的长度

int getCommonStrLength(char * pFirstStr, char * pSecondStr)
{
if (pFirstStr == NULL || NULL == pSecondStr)return 0;
int i, len1, len2, len, s1_start, s2_start, idx, curmax, max;
len1 = strlen(pFirstStr);
len2 = strlen(pSecondStr);
len = len1 + len2;
max = 0;
for (i = 0; i < len; i++)
{
s1_start = s2_start = 0;
if (i < len1)
s1_start = len1 - i; //每次开始匹配的起始位置
else
s2_start = i - len1;
curmax = 0;
for (idx = 0; (s1_start + idx < len1) && (s2_start + idx < len2); idx++)
{
if (toupper(pFirstStr[s1_start + idx]) == toupper(pSecondStr[s2_start + idx]))
curmax++;
else//只要有一个不相等,就说明相等的公共字符断了,不连续了,要保存curmax与max中的最大值,并将curmax重置为0
{
max = curmax > max ? curmax : max;
curmax = 0;
}
}
max = curmax > max ? curmax : max;
}
return max;
}
【求两个字符串的最大公共字符串的长度】转载于:https://www.cnblogs.com/kennyael/p/9987783.html

    推荐阅读