RosettaCodeData/Task/Longest-common-subsequence/C/longest-common-subsequence-1.c

37 lines
893 B
C
Raw Permalink Normal View History

2013-04-10 21:29:02 -07:00
#include <stdio.h>
2016-12-05 22:15:40 +01:00
#include <stdlib.h>
2013-04-10 21:29:02 -07:00
2016-12-05 22:15:40 +01:00
#define MAX(a, b) (a > b ? a : b)
2013-04-10 21:29:02 -07:00
2016-12-05 22:15:40 +01:00
int lcs (char *a, int n, char *b, int m, char **s) {
int i, j, k, t;
int *z = calloc((n + 1) * (m + 1), sizeof (int));
int **c = calloc((n + 1), sizeof (int *));
for (i = 0; i <= n; i++) {
c[i] = &z[i * (m + 1)];
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (a[i - 1] == b[j - 1]) {
c[i][j] = c[i - 1][j - 1] + 1;
2013-04-10 21:29:02 -07:00
}
else {
2016-12-05 22:15:40 +01:00
c[i][j] = MAX(c[i - 1][j], c[i][j - 1]);
2013-04-10 21:29:02 -07:00
}
}
}
2016-12-05 22:15:40 +01:00
t = c[n][m];
*s = malloc(t);
for (i = n, j = m, k = t - 1; k >= 0;) {
if (a[i - 1] == b[j - 1])
(*s)[k] = a[i - 1], i--, j--, k--;
else if (c[i][j - 1] > c[i - 1][j])
j--;
else
i--;
2013-04-10 21:29:02 -07:00
}
2016-12-05 22:15:40 +01:00
free(c);
free(z);
return t;
2013-04-10 21:29:02 -07:00
}