forked from harshsri19992208/DSA-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcs.cpp
51 lines (46 loc) · 752 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
40
41
42
43
44
45
46
47
48
49
50
51
#include<bits/stdc++.h>
using namespace std;
void lengthOfLCS(string s1,string s2)
{
// for calculating the length of lcs
int n=s1.length(),m=s2.length();
int dp[n+1][m+1];
memset(dp,0,sizeof(dp));
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(s1[i-1]==s2[j-1])
dp[i][j]=1+dp[i-1][j-1];
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
cout<<dp[n][m]<<"\n";
// for printing the lcs
string res;
i=n;j=m;
while(dp[i][j]!=0)
{
if(dp[i][j]==max(dp[i-1][j],dp[i][j-1]))
{
if(dp[i][j]==dp[i-1][j])
i--;
else
j--;
}
else
{
res=s1[i-1]+res;
i--;
j--;
}
}
cout<<res;
}
int main()
{
string s1,s2; cin>>s1>>s2;
lengthOfLCS(s1,s2);
}