-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCS(Longest Common SubSequence).cpp
84 lines (63 loc) · 1.45 KB
/
LCS(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
74
75
76
77
78
79
80
81
82
83
84
//Bismillah_hir_rahmanir_rahim
/// algo video = https://www.youtube.com/watch?v=43P0xZp3FU4
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str1,str2;
cin>>str1>>str2;
int len1 = str1.length();
int len2 = str2.length();
int mat [len1+1][len2+1];
for(int i=0; i<=len1; i++)
{
mat[i][0]=0;
}
for(int i=0; i<=len2; i++)
{
mat[0][i]=0;
}
for(int i=1; i<=len1; i++)
{
for(int j=1; j<=len2; j++)
{
if(str1[i-1]==str2[j-1])
{
mat[i][j] = mat[i-1][j-1]+1;
}
else
{
mat[i][j] = max(mat[i-1][j],mat[i][j-1]);
}
}
}
cout<<"The length is:";
cout<<mat[len1][len2]<<endl;
//int index = mat[len1][len2];/// lcs er length
// char LCS[index+1];
// LCS[index] = '\0';
string extra = "";
int i = len1,j=len2;
while(i>0 && j>0)
{
if(str1[i-1] == str2[j-1])
{
extra.push_back(str1[i-1]);
//LCS[index-1]=str1[i-1];
i--;
j--;
//index--;
}
else if(mat[i-1][j] > mat[i][j-1])
{
i--;
}
else
{
j--;
}
}
reverse(extra.begin(),extra.end());
cout<<"LCS is: "<<extra<<endl;
return 0;
}