题解 | #查找两个字符串a,b中的最长公共子串#

查找两个字符串a,b中的最长公共子串

https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506

#include <stdio.h>
#include <string.h>
int main() {
    char stra[300], strb[300], out[300], buf[300] = { 0 };
    int i, j, k = 0, cnt = 0, max = 0;
    scanf("%s", stra);
    scanf("%s", strb);
    if (strlen(stra) > strlen(strb)) {
        strcpy(buf, strb);
        strcpy(strb, stra);
        strcpy(stra, buf);
    }//保证短串一定是stra
    for (i = 0; i < strlen(stra); i++) {
        for (j = 0; j < strlen(strb); j++) {
            i = i - cnt;
            cnt = 0;
            k = 0;
            memset(buf, 0, 300);
            while (stra[i] == strb[j]) {
                buf[k] = stra[i];
                cnt++;
                i++;
                k++;
                j++;
            }
            if (cnt > max) {
                max = cnt;
                strcpy(out, buf);
                j--;
            }
        }
    }

    printf("%s", out);
    return 0;
}

全部评论

相关推荐

06-26 17:24
已编辑
宁波大学 Java
一口洪烧肉:哈哈哈哈哈哈哈哈哈哈哈硬要啊
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务