-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcs.cpp
39 lines (38 loc) · 884 Bytes
/
lcs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <bits/stdc++.h>
using namespace std;
int lcs(string s1, string s2, int m, int n)
{
int arr[m + 1][n + 1];
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
{
arr[i][j] = 0;
}
else if (s1[i - 1] == s2[j - 1])
{
arr[i][j] = arr[i - 1][j - 1] + 1;
}
else
{
arr[i][j] = max(arr[i][j - 1], arr[i - 1][j]);
}
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
return arr[m][n];
}
int main()
{
string s1, s2;
getline(cin, s1);
getline(cin, s2);
int m = s1.size();
int n = s2.size();
cout << "lcs Lenth is : " << lcs(s1, s2, m, n) << endl;
return 0;
}