-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlongest_common_subsequence.cpp
More file actions
77 lines (61 loc) · 1.26 KB
/
Copy pathlongest_common_subsequence.cpp
File metadata and controls
77 lines (61 loc) · 1.26 KB
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
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stack>
#define MAX 100
using namespace std ;
char first[MAX], second[MAX] ;
int dp[MAX][MAX] ;
void printLCS(int , int);
int main ()
{
int len_first , len_second , i , j ;
//getting the input
cout << "Enter the first string " ;
cin >> first ;
cout << "Enter the second string " ;
cin >> second ;
//storing the length of the strings
len_first = strlen(first) ;
len_second = strlen(second) ;
//setting up the dp parameters
for(i = 1 ; i<=len_first; i++){
dp[i][0] = 0 ;
}
for(j = 1; j<=len_second ; j++){
dp[0][j] = 0 ;
}
//making the dp array in bottom up manner
for(i = 1 ; i<=len_first ;i++){
for(j = 1 ; j<=len_second ;j++){
if(first[i-1] == second[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 << "The length of LCS = " << dp[len_first][len_second] ;
cout << "\nLCS : " ;
printLCS(len_first , len_second) ;
return 0 ;
}
void printLCS(int i , int j){
stack <char> s ;
while(i>0 && j>0){
if (first[i-1] == second[j-1]){
s.push(first[i-1]);
i-- ;
j-- ;
}
else if (dp[i-1][j] > dp[i][j-1])
i-- ;
else
j-- ;
}
while(!s.empty()){
cout << s.top() ;
s.pop();
}
}