【发布时间】:2016-03-17 12:33:21
【问题描述】:
我正在尝试进行动态编程来查找 LCS 的长度。我为此使用了二维数组。但是对于一个大字符串,它会由于内存溢出而产生运行时错误。请告诉我我应该如何在一维数组中做以避免内存限制。
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
int max(int a, int b);
int lcs( string X, string Y, int m, int n )
{
int L[m+1][n+1];
int i, j;
for (i=0; i<=m; i++)
{
for (j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i-1] == Y[j-1])
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
return L[m][n];
}
int max(int a, int b)
{
return (a > b)? a : b;
}
int main()
{
string X;
string Y;
cin>>X>>Y;
int m = X.size();
int n = Y.size();
printf("Length of LCS is %d\n", lcs( X, Y, m, n ) );
return 0;
}
【问题讨论】:
-
[OT]
int L[m+1][n+1];使用变长数组扩展,更喜欢使用std::vector。 -
vector 也为 500k 左右的输入字符串提供运行时错误
-
棘手;你可能需要调整Hirshberg’s algorithm 来解决这个问题。这是可行的,但绝对比简单的 LCS 算法复杂得多(编辑:啊,你只对长度感兴趣,而不是实际的子序列;这要容易得多,请参阅 Sergei 的答案)。不相关的是,您肯定需要使用
vector而不是变长数组;另外,打开编译器警告(GCC 和 clang 上的-Wall -Wextra)并使你的编译器变得迂腐(GCC 和 clang 上的-pedantic)。
标签: c++ algorithm data-structures dynamic-programming