内容:用动态规划法实现求两序列的最长公共子序列。
要求:掌握动态规划法的思想,及动态规划法在实际中的应用;分析最长公共子序列的问题特征,选择算法策略并设计具体算法,编程实现两输入序列的比较,并输出它们的最长公共子序列。
#include <iostream>
#include <string>
using namespace std;
int const MaxLen = 50;
class LCS
{
public:
LCS(int nx, int ny, char *x, char *y)
{
m = nx;
n = ny;
a = new char[m + 2];
b = new char[n + 2];
memset(a, 0, sizeof(a)); //将某一块内存中的内容全部设置为指定的值,
memset(b, 0, sizeof(b)); //通常为新申请的内存做初始化工作
for (int i = 0; i < nx + 2; i++)
a[i + 1] = x[i];
for (int i = 0; i < ny + 2; i++)
b[i + 1] = y[i];
c = new int[MaxLen][MaxLen];
s = new int[MaxLen][MaxLen];
memset(c, 0, sizeof(c));
memset(s, 0, sizeof(s));
}
int LCSLength();
void CLCS()
{
CLCS(m, n);
}
private:
void CLCS(int i, int j);
int(*c)[MaxLen], (*s)[MaxLen];
int m, n;
char *a, *b;
};
int LCS::LCSLength() //时间复杂度:O(m×n)
{
for (int i = 1; i <= m; i++)
c[i][0] = 0;
for (int j = 1; j <= n; j++)
c[0][j] = 0;
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (a[i] == b[j])
{
c[i][j] = c[i - 1][j - 1] + 1;
s[i][j] = 1;
}
else if (c[i - 1][j] >= c[i][j - 1])
{
c[i][j] = c[i - 1][j];
s[i][j] = 2;
}
else
{
c[i][j] = c[i][j - 1];
s[i][j] = 3;
}
}
}
return c[m][n];
}
void LCS::CLCS(int i, int j)
{
if (i == 0 || j == 0)
return;
if (s[i][j] == 1)
{
CLCS(i - 1, j - 1);
cout << a[i];
}
else if (s[i][j] == 2)
CLCS(i - 1, j);
else
CLCS(i, j - 1);
}
int main()
{
int nx, ny;
char *x = new char[MaxLen], *y = new char[MaxLen];
cout << "请输入原序列 (不含空格)" << endl;
cin>>x;
nx = strlen(x);
cout << "请输入公共序列 (不含空格)" << endl;
cin >> y;
ny = strlen(y);
LCS lcs(nx, ny, x, y);
cout << "X和Y最长公共子序列的长度为:" << lcs.LCSLength() << endl;
cout << "该序列为" << endl;
lcs.CLCS();
cout << endl;
delete[]x;
delete[]y;
return 0;
}
1.对于线性规划求最长公共字序列的优化:
备忘录方法是动态规划法的一个变种,它采用分治法思想,自顶向下直接递归求最优解。但与分治法不同的是,备忘录方法为每个已经计算的子问题建立备忘录,即保存子问题的计算结果以备需要时应用,从而避免子问题的重复求解。改写当前的int LCSLength()函数,用备忘录方法来求解最长公共子序列。
int LCS::LCSLength(int i, int j)
{
if (i == 0 || j == 0)
return 0;
if (c[i][j] != 0)
return c[i][j];
else
{
if (a[i] == b[j])
{
c[i][j] + LCSLength(i - 1, j - 1) + 1;
s[i][j] = 1;
}
else if (LCSLength(i - 1, j) >= LCSLength(i, j - 1))
{
c[i][j] = LCSLength(i - 1, j);
s[i][j] = 2;
}
else
{
c[i][j] = LCSLength(i, j - 1);
s[i][j] = 3;
}
}
return c[i][j];
}
- 请编写一个类似的CLCS算法实现。不借助二维数组s在O(m+n)的时间内构造最长公共子序列的功能。
void LCS::CLCS(int i, int j)
{
if (i == 0 || j == 0)
return;
if (a[i] == b[j])
{
CLCS(i - 1, j - 1);
cout << a[i];
}
else
{
if (c[i - 1][j] >= c[i][j - 1])
CLCS(i - 1, j);
else
CLCS(i, j - 1);
}
}
3.如果只需计算最长公共子序列的长度,而无须构造最优解,则如何改进原有程序可以使得算法的空间需求大大减少?
请改写原程序,使算法的空间复杂度减少为O(min{m,n})。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define MAX 50
class LCS
{
public:
LCS(int nx, int ny, char *x, char *y)
{
m = nx;
n = ny;
a = new char[m + 1];
b = new char[n + 1];
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (int i = 0; i < nx; i++)
a[i + 1] = x[i];
for (int i = 0; i < ny; i++)
b[i + 1] = y[i];
if (m > n)
{
l = m;
s = n;
}
else
{
char *t;
swap(x, y);
s = m;
l = n;
}
c1 = new int[s + 1];
c2 = new int[s + 1];
memset(c1, 0, sizeof(c1));
memset(c2, 0, sizeof(c2));
}
int LCSLength();
private:
int m, n;
int *c1, *c2;
int l, s;
char *a, *b;
};
int LCS::LCSLength()
{
for (int i = 0; i < s; i++)
c1[i] = 0;
for (int i = 1; i <= l; i++)
{
for (int j = 1; j <= s; j++)
{
if (a[i] == b[j])
c2[j] = c1[j - 1] + 1;
else if (c1[j] >= c2[j - 1])
c2[j] = c1[j];
else
c2[j] = c2[j - 1];
}
for (int j = 0; j < s; j++)
c1[j] = c2[j];
}
return c2[s];
}
int main()
{
int nx, ny;
char *x, *y;
x = new char[MAX];
y = new char[MAX];
cout << "请输入X (不含空格)" << endl;
cin >> x;
nx = strlen(x);
cout << "请输入Y (不含空格)" << endl;
cin >> y;
ny = strlen(y);
LCS lcs(nx, ny, x, y);
cout << "X和Y最长公共子序列的长度为:" << lcs.LCSLength() << endl;
delete[]x;
delete[]y;
return 0;
}