forked from vivekanand44/codes-Youtube-videos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongest Common Subsequence.cpp
74 lines (71 loc) · 1.04 KB
/
Longest Common Subsequence.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1 = "abcvdefgh";
string str2 = "bqdrcvefgh";
int m , n;
m = str1.length();
n = str2.length();
int a[m+1][n+1];
for(int i=0;i<m+1;i++)
{
for(int j=0;j<n+1;j++)
{
a[i][j] = 0;
}
}
int k = 0;
for(int i=1;i<m+1;i++)
{
for(int j=1;j<n+1;j++)
{
if(str1[i-1] == str2[j-1])
{
a[i][j] = a[i-1][j-1] + 1;
}
else
{
if(a[i-1][j] > a[i][j-1])
{
a[i][j] = a[i-1][j];
}
else
{
a[i][j] = a[i][j-1];
}
}
}
}
cout <<"Length of Longest Common Subsequence is" << a[m][n];
//Now let us find out the Longest common subsequence //
int i = m;
int j = n;
char lcs[a[m][n]];
k = a[m][n]-1;
lcs[k] = '\0';
while(i > -1 && j > -1)
{
if(str1[i-1] == str2[j-1])
{
lcs[k] = str1[i-1];
i--;
j--;
k--;
}
else
{
if(a[i-1][j] > a[i][j-1])
{
i = i-1;
}
else
{
j = j-1;
}
}
}
cout <<"\n\n The Longest Common Subsequence is=> " << lcs;
return 0;
}