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

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

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

#include<stdio.h>
#include<string.h>
int main()
{
    char s1[1000], s2[1000], arr[1000];
    scanf("%s %s", s1, s2);
    if(strlen(s1) >strlen(s2))
    {
        char tmp[1000];
        strcpy(tmp, s1);
        strcpy(s1, s2);
        strcpy(s2, tmp);
    }
    int max = 0;
    for(int i=0; i<strlen(s1); i++)
    {
        for(int j=0; j<strlen(s2); j++)
        {
            int n = 0;
            while(s1[i+n] == s2[j+n] && s1[i+n] != '\0')
            {
                n++;
            }
            if(n > max)
            {
                max = n;
                strcpy(arr, s1+i);
                arr[max] = '\0';     
            }
        }
    }
    printf("%s\n", arr);
    return 0;
}
全部评论

相关推荐

用微笑面对困难:只要你保证项目和获奖都是真的就行尤其是“对战,总负责人”啊这些套职,基本上队员,打杂的都这么写
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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