代码
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
const int MAX = 100;
const int m = 7, n = 6;
int C[MAX][MAX]; // C[i][j]:Xi和Yj的LCS长度
int X[m] = {'A', 'B', 'C', 'B', 'D', 'A', 'B'}, Y[n] = {'B', 'D', 'C', 'A', 'B', 'A'};
void LCS(int X[], int Y[], int m, int n){
int i, j;
char B[MAX][MAX]; // B[i][j]:C[i][j]取得最大值的标志
for (i = 1; i <= m; i++)
C[i][0] = 0;
for (i = 1; i <= n; i++)
C[0][i] = 0;
for (i = 1; i <= m; i++){
for (j = 1; j <= n; j++){
if (X[i] == Y[j]){
C[i][j] = C[i - 1][j - 1] + 1;
B[i][j] = 'lu';
} else if (C[i-1][j] >= C[i][j-1]){
C[i][j] = C[i - 1][j];
B[i][j] = 'u';
} else {
C[i][j] = C[i][j - 1];
B[i][j] = 'l';
}
}
}
printf("
C[i][j]
");
for (i = 1; i <= m; i++){
printf("%d ", i);
for (j = 1; j <= n; j++){
printf("%d ", C[i][j]);
}
printf("
");
}
printf("
B[j][j]
");
for (i = 1; i <= m; i++){
printf("%d ", i);
for (j = 1; j <= n; j++){
printf("%c ", B[i][j]);
}
printf("
");
}
}
int main () {
LCS(X, Y, m, n);
return 0;
system("pause");
}