动态规划法
1.POJ1458
求最长公共子序列问题(LCS),自底向上打印一个矩阵即可。
代码:
#include <iostream>
#include <string>
using namespace std;
//自底向上的动态规划求最长公共子序列(LCS),也可以用递归
int main(){
int LCS[1000][1000];
string X,Y;
while (cin >> X >> Y) {
int m = int(X.length());
int n = int(Y.length());
for (int i = 0; i <= m; i++)
LCS[i][0] = 0;
for (int j = 0; j <= n; j++)
LCS[0][j] = 0;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if(X[i-1] == Y[j-1])
LCS[i][j] = LCS[i-1][j-1] + 1;
else
{
if (LCS[i-1][j] >= LCS[i][j-1])
LCS[i][j] = LCS[i-1][j];
else
LCS[i][j] = LCS[i][j-1];
}
}
}
cout << LCS[m][n] << endl;
}
return 0;
}
2.POJ1050
题目:
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
As an example, the maximal sub-rectangle of the array:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:
9 2
-4 1
-1 8
and has a sum of 15.
算法思路:
求矩阵的最⼤块问题,思路是将⼆维矩阵“拍扁”,当成一维数组的最大连续段问题来做。
一维数组的最大连续段问题:</
5733




被折叠的 条评论
为什么被折叠?



