没什么思路,就是无脑干
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a, b;
cin >> a >> b;
int alen = a.length(), blen = b.length();
if (alen > blen)
{
swap(a,b);//确保a是最小的字符串
}
alen = a.length();
blen = b.length();
string LongestSubstr = "";
int MostSubsterLen = 0;
for (int i = 0;i < alen;i++)//字串的长度
{
string atemp = "";
string btemp = "";
for (int z = 0;z < alen-i;z++)//从左往右遍历a
{
atemp = a.substr(z, i+1);
bool flag = false;
for (int j = 0;j < blen-i;j++)//从左往右遍历b,判断atemp是否也是b的字串
{
btemp = b.substr(j, i + 1);
if (atemp == btemp)
{
flag = true;
break;
}
}
if (flag)
{
if (atemp.length() > MostSubsterLen)
{
LongestSubstr = atemp;
MostSubsterLen = LongestSubstr.length();
}
}
}
}
cout << LongestSubstr << endl;
cout << MostSubsterLen << endl;
return 0;
}
PS:应该是确保a是最短的字符串
感觉在某些地方还可以优化。
无所谓了。