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

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

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

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    string str1;
    string str2;
    cin >> str1 >> str2;
    if (str1.size() > str2.size())   //str1是短的字串,str2是长的字串
    {
        string temp = str1;
        str1 = str2;
        str2 = temp;
    }

    string comm_str;  //提取出的公共字符chuan
    int maxstr = 0;   //最大字符串长度

    for (int i = 0;i < str1.size();i++)
    {
        for (int j = 1;j <= str1.size() - i;j++)
        {
            string newstr = str1.substr(i,j);
            if (str2.find(newstr) != string::npos)
            {
                if (newstr.size() > maxstr)
                {
                    comm_str = newstr;
                    maxstr = newstr.size();
                }
            }
        }
    }

    cout << comm_str;


}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

我是没经验的毕业生,这啥情况啊会不会是hr在刷kpi
JamesGosli...:字节boss属于是群发了,我都快入职字节了,其他部门还在和我boss打招呼
点赞 评论 收藏
分享
牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
完美的潜伏者许愿简历...:隐藏信息被你提取出来了,暗示,这就是暗示
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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