我的面试题,代码因为紧张写的有错误,更正了发出来,大家看看吧,可能5期与我无缘了,分享出来大家学习
checknext(char *str1,char *str2,int i,int j,int *length)
{
if(str1[i+1]==str2[j+1]&&str[i+1]!='\0'&&str2[j+1]!='\0')
{
(*length)++;
checknext(str[i+1],str[j+1],i+1,j+1,length); // 递归查找下一个相同的字符
}
}
(void)commStr(char *str1, char *str2)
{
char *longstr;
char *shortstr;
int length = 0;
int max = 0;
int start = 0;
if(str1.length>str2.length)
{
longstr = str1;
shortstr = str2;
}
else
{
longstr = str2;
shortstr = str1;
}
for(int i=0;i<longstr.length;i++) // 遍历
{
for(int j =0;j<shortstr.length;j++)
{
if(longstr[i]==shortstr[j])
{
length = 1;
checknext(longstr,shortstr,i,j,&length);
if(length>max)
{
max = length;
start = i;
}
}
}
}
if(length == 0)
{
printf("没有找到最大公共字符串!\0");
}
else
{
for(int i=start;i<(max+length);i++) // 输出
{
printf("最大公共字符串是:%c\0",longstr[i]);
}
}
}
int main()
{
char *a = "nbitheimanb";
char *b = "itheima";
commStr(a,b);
return 0;
} |