-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCS.cpp
54 lines (50 loc) · 1.09 KB
/
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
52
53
54
/*
Given two strings, we've to find the longest common subsequence among that.
We start with the iteration of both the strings and keep moving our pointers according to the condition they satisfy.
*/
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
string s , t ;
cout << "Enter string one : " ;
cin >> s ;
cout << "Enter string two : " ;
cin >> t ;
int n = s.length() ;
int m = t.length() ;
vector<vector<int>> dp (n+1 , vector<int> (m+1,0));
for(int i=1; i<=n ; i++)
{
for(int j=1; j<=m ; j++)
{
if(s[i-1] == t[j-1])
{
dp[i][j] = dp[i-1][j-1] + 1 ;
}
else{
dp[i][j] = max(dp[i-1][j] , dp[i][j-1]);
}
}
}
cout << "Length of LCS is :- " << dp[n][m] << endl ;
string ans="" ;
while(n>0 && m>0)
{
if(s[n-1]==t[m-1])
{
ans+=s[n-1] ;
n-- ; m-- ;
}
else
{
if(dp[n][m-1] > dp[n-1][m])
{
m-- ;
}
else n-- ;
}
}
reverse(ans.begin(),ans.end());
cout << "LCS is :- " << ans << endl;
}