LCS
#include<cstdio>
#include<cstring>
#include<iostream>
#include <vector>
using namespace std;
int main(){
string str1,str2;
cout<<"Input str1:"<<endl;
cin>>str1;
cout<<"Input str2:"<<endl;
cin>>str2;
int len1=str1.length();
int len2=str2.length();
int **n=new int*[len1];
for(int i=0;i<len1;++i){
*(n+i)=new int [len2];
}
for(int i=0;i<len1;++i)
for(int j=0;j<len2;++j)
n[i][j]=0;
int maxlen=0;
vector<int> vec;
for(int i=0;i<len1;++i){
for(int j=0;j<len2;++j){
if(str1[i]==str2[j]){
if(i==0 || j==0)
n[i][j]=1;
else{
n[i][j]+=n[i-1][j-1]+1;
if(n[i][j]>maxlen){
maxlen=n[i][j];
vec.clear();
vec.push_back(i);
}else if(n[i][j]==maxlen){
vec.push_back(i);
}
}
}
}
}
for(int i=0;i<len1;++i){
for(int j=0;j<len2;++j)
cout<<n[i][j];
cout<<endl;
}
cout<<"All Common Substring:"<<endl;
for(int i=0;i<vec.size();++i){
for(int j=vec[i]-maxlen+1;j<=vec[i];++j){
cout<<str1[j];
}
cout<<endl;
}
return 0;
}