#include<bits/stdc++.h>
using namespace std;
int lcs(string s1, string s2, int n, int m)
{
int L[n+1][m+1];
int i, j;
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(i == 0 || j == 0)
L[i][j] = 0;
else if(s1[i-1] == s2[j-1])
L[i][j] = 1 + L[i-1][j-1];
else
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
return L[n][m];
}
int main()
{
string s1, s2;
cout << "Enter First String :: ";
cin >> s1;
cout << "Enter Second String :: ";
cin >> s2;
int n = s1.size();
int m = s2.size();
cout << endl << "Longest Common Sub-Sequence Size is :: ";
cout << lcs(s1, s2, n, m) << endl;
return 0;
}
Comments
Post a Comment