-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCS.cpp
73 lines (63 loc) · 1.4 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//----------------Longest Common Subsequence-----------------------------------
//-----------------O(mn) approach---------------------------------------------
#include<iostream>
#define MAX 1000
using namespace std;
int arr[MAX][MAX];
char sub[MAX]; // to store subsequence
int max(int a,int b) {
if(a>b)
return a;
return b;
}
char * reconstruct (const string p,const string q) { // to construct the subsequence from array
int m=q.length();
int n=p.length();
int i=m,j=n; // i is the row and j is the column
int k=0;
while(i>0 && j>0) {
// cout << i << " " << j << endl;
if(q[i-1] == p[j-1]) {
sub[arr[m][n]- k-1] = q[i-1];
// cout << k << " " << arr[m][n]- k << endl;
k++;
i--;
j--;
}
else if (arr[i][j-1] > arr[i-1][j]) {
j--;
}
else i--;
}
sub[k]='\0';
return sub ;
}
char * lcs (const string p,const string q) {
for (int i=1;i<=q.length();i++) {
for(int j=1;j<=p.length();j++) {
if(q[i-1]==p[j-1]) {
arr[i][j]=arr[i-1][j-1]+1;
}
else {
arr[i][j]=max(arr[i-1][j],arr[i][j-1]);
}
}
}
return reconstruct(p,q);
}
main() {
string p,q;
cout << "enter the strings: " << endl;
cin >> p >> q;
cout << "lcs is\n";
cout << lcs(p,q);
cout << endl;
//cout << p.length() << " " << q.length() << endl;
/* for (int i=1;i<=q.length();i++) {
for(int j=1;j<=p.length();j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
*/
}