#include<iostream>
#include<vector>
using namespace std;
int maxcaculate(string a,string b,string *&c){
int m,n,th;
int count1=0,maxx=0;
for(int i=0;i<a.length();i++){
for(int j=0;j<b.length();j++){
m=i;
n=j;
while(a[m]==b[n] && m<a.length() &&n<b.length()){
m++;
n++;
count1++;
}
if(count1>maxx){
th=i;
maxx=count1;
}
count1=0;
}
}
c=new string[maxx+1];
for(int i=0;i<maxx;i++){
c[i]=a[i+th];
}
c[maxx]='\0';
return maxx;
}
struct jie{
int result;
int n;
};
struct jie lcs(string str1, string str2) {
int len1 = str1.length();
int len2 = str2.length();
int result = 0;
int n=0; //记录最长公共子串长度
vector<vector<int> > c(len1+1,vector<int>(len2+1,0));
for (int i = 0; i <= len1; i++) {
for( int j = 0; j <= len2; j++) {
if(i == 0 || j == 0) {
c[i][j] = 0;
} else if (str1[i-1] == str2[j-1]) {
c[i][j] = c[i-1][j-1] + 1;
//result = max(c[i][j], result);
if(result<c[i][j]){
result=c[i][j];
n=i;
}
} else {
c[i][j] = 0;
}
}
}
return {result,n};
}
int main(){
string str1="qwertyuio";
string str2="wert";
struct jie r=lcs(str1,str2);
//int length=maxcaculate(str1,str2,str);
//for(int i=0;i<length;i++){
// cout<<str[i];
//}
cout<<r.result<<endl;
for(int i=r.n-r.result;i<r.n;i++){
cout<<str1[i];
}
return 0;
}
返回了两个值,最长子字符串的长度 和 最长子字符串