马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdlib.h>
#include <stdio.h>
void LCSLength(int m, int n, char *x, char *y, int **c)
{
int i, j;
//初始化二维数组
for (i = 1; i <= m; i++)
{
c[i][0] = 0;
printf("%d ", c[i][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;
else if
(c[i - 1][j] >= c[i][j - 1]) c[i][j] = c[i - 1][j];
else
c[i][j] = c[i][j - 1];
}
for (i = 0; i <= m; i++)
{
for (j = 0; j <= n; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
}
int main()
{
char a[] = { 1, 3, 4, 5, 6,7, 7, 8,'\0' };
char b[] = { 3, 5, 7, 4, 8,6,7,8,2,'\0' };
char *x = &a[0];
char *y = &b[0];
int m = sizeof(a) - 1; //X元素序列的个数
int n = sizeof(b) - 1;//Y元素序列的个数
//为二维数组申请动态空间,m为行数,n为列数
int **c = (int **)malloc(sizeof(int *) * m); //分配行数
for (int i = 0; i < m; i++) //为每一行分配内存
{
c[i] = (int*)malloc(sizeof(int)*n);
}
printf("-------\n");
LCSLength(m, n, x, y, c);
printf("-------\n");
printf("%d\n", c[m][n]);
}
报错如图 |