编制一个函数,实现在两个已知字符串中找出所有非空最长公共子串的长度和最长公共子串的个数
#include
<
string
.h
>
#include < stdio.h >
int commStr( char * str1, char * str2, int * lenpt)
{
int len1,len2,ln,count,i,k,p;
char * st;
if ((len1 = strlen(str1)) < (len2 = strlen(str2))) {
st = str1; str1 = str2; str2 = st;
ln = len1; len1 = len2; len2 = ln;
}
count = 0 ;
for (ln = len2;ln > 0 ;ln -- ) {
for (k = 0 ;k + ln <= len2;k ++ ) {
for (p = 0 ;p + ln <= len1;p ++ ) {
for (i = 0 ;i < ln;i ++ )
if (str2[k + i] != str1[p + i]) break ;
if (i == ln)
count ++ ;
}
}
if (count) break ;
}
* lenpt = ln;
return count;
}
int main()
{
int c,len;
c = commStr( " Abc1AbcsAbcd123 " , " 123bAbc " , & len);
printf( " 有%d个长为%d的公共子串\n " ,c,len);
}
#include < stdio.h >
int commStr( char * str1, char * str2, int * lenpt)
{
int len1,len2,ln,count,i,k,p;
char * st;
if ((len1 = strlen(str1)) < (len2 = strlen(str2))) {
st = str1; str1 = str2; str2 = st;
ln = len1; len1 = len2; len2 = ln;
}
count = 0 ;
for (ln = len2;ln > 0 ;ln -- ) {
for (k = 0 ;k + ln <= len2;k ++ ) {
for (p = 0 ;p + ln <= len1;p ++ ) {
for (i = 0 ;i < ln;i ++ )
if (str2[k + i] != str1[p + i]) break ;
if (i == ln)
count ++ ;
}
}
if (count) break ;
}
* lenpt = ln;
return count;
}
int main()
{
int c,len;
c = commStr( " Abc1AbcsAbcd123 " , " 123bAbc " , & len);
printf( " 有%d个长为%d的公共子串\n " ,c,len);
}