编辑代码

#include <stdio.h>
#include <string.h>
int max(int a, int b) {
    return (a > b) ? a : b;
}
int maxCommonSubsequence(char s1[], char s2[]) {
    int m = strlen(s1);
    int n = strlen(s2);
    int dt[m + 1][n + 1];
    for (int i = 0; i <= m; i++) {
        for (int j = 0; j <= n; j++) {
            if (i == 0 || j == 0) {
                dt[i][j] = 0;
            } else if (s1[i - 1] == s2[j - 1]) {
                dt[i][j] = dt[i - 1][j - 1] + 1;
            } else {
                dt[i][j] = max(dt[i - 1][j], dt[i][j - 1]);
            }
        }
    }
    return dt[m][n];
}
int main() {
    char s1[] = "ADBCAB";
    char s2[] = "BADB";
    int result = maxCommonSubsequence(s1, s2);
    printf("最大公共子序列长度: %d\n", result);
    return 0;
}