动态规划lcs problemc语言实现
时间: 2023-12-13 11:33:53 浏览: 78
C语言实现动态规划算法
5星 · 资源好评率100%
以下是C语言实现动态规划解决LCS问题的代码:
```c
#include <stdio.h>
#include <string.h>
int max(int a, int b) {
return (a > b) ? a : b;
}
void lcs(char *X, char *Y, int m, int n) {
int L[m + 1][n + 1];
int i, j;
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
L[i][j] = 0;
} else if (X[i - 1] == Y[j - 1]) {
L[i][j] = L[i - 1][j - 1] + 1;
} else {
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
}
int index = L[m][n];
char lcs[index + 1];
lcs[index] = '\0';
i = m;
j = n;
while (i > 0 && j > 0) {
if (X[i - 1] == Y[j - 1]) {
lcs[index - 1] = X[i - 1];
i--;
j--;
index--;
} else if (L[i - 1][j] > L[i][j - 1]) {
i--;
} else {
j--;
}
}
printf("LCS of %s and %s is %s", X, Y, lcs);
}
int main() {
char X[] = "AGGTAB";
char Y[] = "GXTXAYB";
int m = strlen(X);
int n = strlen(Y);
lcs(X, Y, m, n);
return 0;
}
```
阅读全文