给出两个字符串,求出两个字符串的最长公共子串
#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,所需字符个数
返回值:一个子字符串,从其指定位置开始