题解 | #最长公共子串#

最长公共子串

http://www.nowcoder.com/practice/210741385d37490c97446aa50874e62d

//暴力解
#include<bits/stdc++.h>
using namespace std;
int main(){
    string str1,str2;
    cin>>str1>>str2;
    int max=0,pos=-1;
    int N=str1.size(),M=str2.size();
    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++){
            int count=0;
            int x=i,y=j;
            while(str1[x]==str2[y]&&x<N&&y<M){
                count++;
                x++;
                y++;
            }
            if(count>max){
                max=count;
                pos=i;
            }
        }
    }
    if(pos==-1){
        cout<<-1<<endl;
        return 0;
    }
    else{
        for(int i=pos;i<pos+max;i++)
            cout<<str1[i];
        return 0;
    }
}
全部评论

相关推荐

06-26 17:24
已编辑
宁波大学 golang
迷失西雅图:别给,纯kpi,别问我为什么知道
点赞 评论 收藏
分享
牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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