-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcs_memo.c
69 lines (42 loc) · 1.09 KB
/
lcs_memo.c
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
#include <stdio.h>
#include <string.h>
int max(int a, int b);
int lcs(char *x, char *y, int m, int n, int dp[m][n])
{
if ( m == 0 || n == 0 ) {
return 0;
} else if ( dp[m][n] != -1 ) {
return dp[m][n];
} else if ( x[m] == y[n] ) {
dp[m][n] = 1 + lcs(x, y, m - 1, n - 1, dp);
return dp[m][n];
} else {
dp[m][n] = max(lcs(x, y, m - 1, n, dp), lcs(x, y, m, n - 1, dp));
return dp[m][n];
}
}
int max(int a, int b)
{
return (a > b)? a:b;
}
int main()
{
char s1[] = "AGGTAB";
char s2[] = "GXTXAYB";
int m = strlen(s1); //6
int n = strlen(s2); //7
int a[m][n];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
a[i][j] = -1;
}
}
printf("%d\n",lcs(s1, s2, m, n, a));
// lcs(s1, s2, m, n, a);
// for (int i = 1; i <= m; i++) {
// for (int j = 1; j <= n; j++) {
// printf("%d ",a[i][j]);
// }
// printf("\n");
// }
}