资源简介
C++实现找出两个字符串中最大的公共子串
代码片段和文件信息
#include
#include
#include
using namespace std;
//find the max one of sub strings which are both in two strings
void FindMaxsubstr(const char *strA const char *strB char *maxSubStr);
int main()
{
char substr[20];
char str1[] = “abcdefghi“;
char str2[] = “acdeghfabcde“;
FindMaxsubstr(str1 str2 substr);
cout<
return 0;
}
void FindMaxsubstr(const char *strA const char *strB char *maxSubStr)
{
assert((strA != NULL) && (strB != NULL));
assert(maxSubStr != NULL);
int maxPos = -1;
int maxLen = 0;
for(int i = 0; i < strlen(strA); i++)
{
for(int j = 0; j < strlen(strB); j++)
{
if (strA[i] == strB[j])
{
for (int k = 1; (strA[i+k] == strB[j+k])&&(strA[i+k]!=‘\0‘); )
{
if (k >= maxLen)
{
maxPos = i;
maxLen = k + 1;
}
k++;
}
}
}
}
cout<<“maxPos = “< if (maxPos == - 1)
{
maxSubStr[0] = ‘\0‘;
}
else
{
memcpy(maxSubStr strA + maxPos maxLen);
maxSubStr[maxLen] = ‘\0‘;
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1104 2012-10-17 19:33 FindMaxSubString\FindMaxSubString.cpp
文件 3964 2012-10-17 18:40 FindMaxSubString\FindMaxSubString.vcproj
文件 1427 2012-10-17 19:37 FindMaxSubString\FindMaxSubString.vcproj.PC-201207101739.Administrator.user
目录 0 2012-10-17 19:43 FindMaxSubString
----------- --------- ---------- ----- ----
6495 4
- 上一篇:C语言的数学函数库,所有的C语言算法,经典算法
- 下一篇:网络电话的C++源代码
评论
共有 条评论