-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC Subsequence
34 lines (29 loc) · 957 Bytes
/
LC Subsequence
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
#include<bits/stdc++.h>
#define MAX 1000
using namespace std;
int m, n;
int c[MAX][MAX], b[MAX][MAX];
int LCSlength(string x, string y){
int m, n;
m = x.size();
n = y.size();
for(int i=1; i<=m; i++) c[i][0] = 0;
for(int i=0; i<=n; i++) c[0][i] = 0;
for(int i=1; i<=m; i++){
for(int j=1; j<=n; j++){
if(x[i-1] == y[j-1]){
c[i][j] = c[i-1][j-1] + 1;
b[i][j] = 1;
}
else if(c[i-1][j] >= c[i][j-1]){
c[i][j] = c[i-1][j];
b[i][j] = 2; //from north
}
else{
c[i][j] = c[i][j-1];
b[i][j] = 3;//from west
}
}
}
return c[m][n];
}