在过去的两个小时里,我一直被这个小虫子困住,现在我绝望了。非常感谢任何帮助!
该算法用于为Longest Common Subsequence
两个序列之间的问题制作矩阵。并且该矩阵被用作动态规划方法中的参考表。粘贴相关代码
#include <vector>
#include <cstdio>
#include <math.h>
using namespace std;
int main()
{
vector<int> A, B;
A.push_back(0);
A.push_back(15);
A.push_back(20);
B.push_back(0);
B.push_back(20);
B.push_back(15);
int a = 2;
int b = 2;
int matrix[a][b];
memset(matrix, 0, sizeof(int)*a*b);
for (int i = 0; i <= a; ++i)
{
for (int j = 0; j <= b; ++j)
{
if (i == 0 || j == 0)
{
matrix[i][j] = 0;
} else
{
if (A[i] == B[j])
{
matrix[i][j] = matrix[i - 1][j - 1] + 1;
printf("matrix at row %i column %i: %i\n", i, j, matrix[i][j]);
} else
{
matrix[i][j] = max(matrix[i - 1][j], matrix[i][j - 1]);
}
}
}
}
printf("matrix at row 1 column 2: %i\n", matrix[1][2]);
printf("matrix at row 2 column 1: %i\n", matrix[2][1]);
}
如果我编译并运行它
g++ -Wall soquestion1.cpp -o soquestion1
./soquestion1
我明白了
matrix at row 1 column 2: 1
matrix at row 2 column 1: 1
matrix at row 1 column 2: 0 #WTHHHHHH, who changed my matrix!?
matrix at row 2 column 1: 1
感谢您阅读那么远。