题解 | #查找两个字符串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;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 11:29
点赞 评论 收藏
分享
06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
人力小鱼姐:实习经历没有什么含金量,咖啡店员迎宾这种就别写了,其他两段包装一下 想找人力相关的话,总结一下个人优势,结合校园经历里有相关性的部分,加一段自我评价
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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