1. 程式人生 > >莫使金樽空對月-位元組跳動軟體測試開發工程師一面演算法題

莫使金樽空對月-位元組跳動軟體測試開發工程師一面演算法題

給出兩個字串,求出兩個字串的最長公共子串
心塞塞,一面之後沒有進入二面,菜是原罪。

#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,所需字元個數
返回值:一個子字串,從其指定位置開始

不知道自己是多少次被拒絕了,很難受,對自己很失望。