在此处说明一下,这道题的思路来源于在网上参考的别人的链接,感谢大佬们的指导~
https://blog.csdn.net/qq_25800311/article/details/81607168
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1,str2;
while(cin>>str1>>str2)
{
int len1=str1.length();
int len2=str2.length();
int max=0;
int record[100][100];
for (int i=0;i<len1;i++)
{
for (int j=0;j<len2;j++)
{
if (str1[i]==str2[j])
{
if (i==0 || j==0)
{
record[i][j]=1;
}
else
{
record[i][j]=record[i-1][j-1]+1;
}
}
else
{
record[i][j]=0;
}
if (record[i][j]>max)
max=record[i][j];
}
}
cout<<max<<endl;
}
return 0;
}
这里的重点还是关于构建二维数组的方法,需要仔细看看别人的做法是怎么样~