#include<iostream>
#include<vector>
#include<string>
using namespace std;
void display(vector<string>vt,int number,string str)//在短字符串中输出最先出现的最长子串
{
int index;
int min_value=10000000;
int location;
for(int i=0;i<vt.size();i++)
{
if(vt[i].size()==number)
{
index=str.find(vt[i]);
if(index<min_value)
{
min_value=index;
location=i;
}
}
}
cout<<vt[location]<<endl;
}
int main()
{
string str1,str2;
while(cin>>str1>>str2)
{
vector<string>vt;
int count=-1;
int max_value=0;
for(int i=0;i<str1.size();i++)
{
int k=i;
string tmp;
for(int j=0;j<str2.size();j++)
{
while(str1[k]==str2[j])
{
if(k<str1.size() && j<str2.size()){
tmp+=str1[k];
k++;
j++;
}
else
break;
}
if(tmp.size()>0)
{
vt.push_back(tmp);
if(tmp.size()>max_value)
{
max_value=tmp.size();
count++;
}
else
{
count++;
}
tmp.clear();
}
k=i;
}
}
string str;
str1.size()<str2.size()?display(vt,max_value,str1):display(vt,max_value,str2);
}
}
华为机试-查找两个字符串a,b中的最长公共子串
最新推荐文章于 2021-03-18 11:12:24 发布