image

编辑人: 未来可期

calendar2025-03-09

message6

visits765

找出两个字符串中最大公共子字符串,如”abccade”,”dgcadde”的最大子串为”cad”

int GetCommon(char *s1, char *s2, char **r1, char **r2)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
int maxlen = 0;
for(int i = 0; i < len1; i++) { for(int j = 0; j < len2; j++) { if(s1[i] == s2[j]) { int as = i, bs = j, count = 1; while(as + 1 < len1 && bs + 1 < len2 && s1[++as] == s2[++bs]) count++; if(count > maxlen)
{
maxlen = count;
*r1 = s1 + i;
*r2 = s2 + j;
}
}
}
}

喵呜刷题:让学习像火箭一样快速,快来微信扫码,体验免费刷题服务,开启你的学习加速器!

创作类型:
原创

本文链接:找出两个字符串中最大公共子字符串,如”abccade”,”dgcadde”的最大子串为”cad”

版权声明:本站点所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明文章出处。
分享文章
share