#include
#include
#include
/*
查找最大公共子串
*/
char *maxsubstr(char *A,char *B)
{
char *aptr,*bptr,*start_ptr;
int count=0,sublen=0;
char *substr;
for (int i=0;A[i]!='\0';i )
{
for (int j=0;B[j]!='\0';j )
{
if (A[i]==B[j])
{
aptr = &A[i];
bptr = &B[j];
while (*aptr!='\0'&&*bptr!='\0')
{
if (* aptr!=* bptr)
{
count=aptr-&A[i];
break;
}
}
if (sublen {
sublen=count;
start_ptr=&A[i];
}
}
}
}
substr = (char *)malloc(sizeof(char)*sublen 1);
substr[sublen]='\0';
strncpy(substr,start_ptr,sublen);
return substr;
}
int main()
{
char A[]="a***sdfgsdsfff";
char B[]="asdesdfgsxsfff";
char *substr=maxsubstr(A,B);
printf("max sublen is %d ,max substr is %s\n",strlen(substr),substr);
return 0;
}
阅读(1556) | 评论(0) | 转发(0) |