莫使金樽空对月-字节跳动软件测试开发工程师一面算法题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Betterc5/article/details/84001089

给出两个字符串,求出两个字符串的最长公共子串
心塞塞,一面之后没有进入二面,菜是原罪。

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string a, b;
    while (cin >> a >> b)
    {
        if (a.size() > b.size())
            swap(a, b);
        string str_m;//存储最长公共子串
        for (int i = 0; i < a.size(); i++)
        {
            for (int j = i; j < a.size(); j++)
            {
                string temp = a.substr(i, j - i + 1);
  
                    if (int(b.find(temp))<0)
                    break;
                else if (str_m.size() < temp.size())
                    str_m = temp;
            }
        }
        cout << str_m << endl;
    }
    return 0;
}

basic_string substr(size_type _Off = 0,size_type _Count = npos) const;
_Off,所需字符串的起始位置
_Count,所需字符个数
返回值:一个子字符串,从其指定位置开始

不知道自己是多少次被拒绝了,很难受,对自己很失望。

猜你喜欢

转载自blog.csdn.net/Betterc5/article/details/84001089