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

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

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

#include<stdio.h>
#include<string.h>
int main()
{
    char arr1[300]="";
    char arr2[300]="";
    char temp[300]="";
    while(scanf("%s %s",arr1,arr2)!=EOF)
    {
        if(strlen(arr1)>strlen(arr2))
        {
            strcpy(temp,arr1);
            strcpy(arr1,arr2);
            strcpy(arr2,temp);
        }
        int max=0;
        for(int i=0;i<strlen(arr1);i++)
            for(int j=0;j<strlen(arr2);j++)
            {
                int count=0;
                while(arr1[i+count]==arr2[j+count])
                    count++;
                if (count>max)
                {
                    max=count;
                    strcpy(temp,arr1+i);
                    temp[max]='\0';
                }
            }
        printf("%s\n",temp);
    }
    
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 10:39
一个证都没&nbsp;我能填什么
程序员小白条:别人有,你为什么没有,还是这个道理,社会就是比较,竞争,淘汰,你要安逸,那么就要做好淘汰的准备
点赞 评论 收藏
分享
07-01 23:23
郑州大学 Java
否极泰来来来来:牛客迟早有高三的
点赞 评论 收藏
分享
06-12 10:50
门头沟学院 Java
你的不定积分没加C:我怎么在学院群看到了同样的话
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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