一:LCS问题
#include "iostream"
#include "math.h"
using namespace std;
class LCS
{
public:
LCS(int nx, int ny, char *a, char *b);
~LCS();
int LCSLength();
int LCSLength_Memo();
void CLCS();
private:
int LCSLength_Memo(int i,int j);
void CLCS(int i, int j);
int **c, **s, m, n;
char *x, *y;
};
LCS::LCS(int nx, int ny, char *a, char *b)
{
m=nx; n=ny;
x=a; y=b;
c=new int*[m+1]; s=new int*[m+1];
for(int i=0; i<=m; i++)
{
c[i]=new int [n+1];
s[i]=new int [n+1];
}
}
LCS::~LCS()
{
for(int i=0; i<=m; i++)
{
delete []c[i];
delete []s[i];
}
delete []c; delete []s;
}
int LCS::LCSLength()
{
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 (x[i] == y[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()
{ CLCS(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 << x[i];
}
else if (s[i][j] == 2)
CLCS(i - 1, j);
else
CLCS(i, j - 1);
}
int LCS::LCSLength_Memo()
{
memset(c, INT_MAX, sizeof(c));
return LCSLength_Memo(m, n);
}
int LCS::LCSLength_Memo(int i,int j)
{
if (c[i][j] < INT_MAX)
return c[i][j];
if ((i == 0) || (j == 0))
c[i][j] = 0;
else if (x[i - 1] == y[j - 1])
c[i][j] = LCSLength_Memo(i - 1, j - 1) + 1;
else
{
int p = LCSLength_Memo(i - 1, j);
int q = LCSLength_Memo(i, j - 1);
if (p >= q)
c[i][j] = p;
else
c[i][j] = q;
}
return c[i][j];
}
void main()
{
int nx = 7, ny = 6, len1, len2;
char x[] = "0abcbdab", y[] = "0bdcaba";
LCS LL(nx, ny, x, y);
cout << "x[]=\"0abcbdab\"; y[]=\"0bdcaba\"" << endl << endl;
cout << "1. 不用备忘录算法求LCS的长度:" << endl;
len1 = LL.LCSLength();
cout << " LCS的长度=" << len1 << endl << endl;
cout << "2. 用备忘录算法求LCS的长度:" << endl;
len2 = LL.LCSLength_Memo();
cout << " LCS的长度=" << len2 << endl << endl;
cout << "3. LCS=\"";
LL.CLCS();
cout << "\"" << endl;
}
二:矩阵连乘问题
#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
class MatrixChain
{ public:
MatrixChain(int mSize, int *q);
int MChain();
int LookupChain();
void Traceback();
void Output();
private:
void Traceback(int i, int j);
int LookupChain(int i, int j);
int *p, **m, **s, n;
};
MatrixChain::MatrixChain(int mSize, int *q)
{ n=mSize;
m=new int*[n]; s=new int*[n]; p=new int[n+1];
for(int i=0; i<n; i++)
{ m[i]=new int [n]; m[i][i]=0;
s[i]=new int [n]; s[i][i]=0;
p[i]=q[i];
}
p[n]=q[n];
}
int MatrixChain::MChain()
{
for (int i = 0; i < n; i++)
m[i][i] = 0;
for (int r = 2; r <= n; r++) {
for (int i = 0; i <= n - r; i++) {
int j = i + r - 1;
m[i][j] = m[i + 1][j] + p[i] * p[i + 1] * p[j + 1];
s[i][j] = i;
for (int k = i + 1; k < j; k++) {
int t = m[i][k] + m[k + 1][j] + p[i] * p[k + 1] * p[j + 1];
if (t < m[i][j]) {
m[i][j] = t;
s[i][j] = k;
}
}
}
}
return m[0][n - 1];
}
void MatrixChain::Traceback(int i, int j)
{
if (i == j) {
cout << "A" << i;
return;
}
if (i < s[i][j]) cout << "(";
Traceback(i, s[i][j]);
if (i < s[i][j]) cout << ")";
if (s[i][j] + 1 < j) cout << "(";
Traceback(s[i][j] + 1, j);
if (s[i][j] + 1 < j) cout << ")";
}
void MatrixChain::Traceback()
{
cout << "(";
Traceback(0, n - 1);
cout << ")";
cout << endl;
}
int MatrixChain::LookupChain(int i, int j)
{
if (m[i][j] > 0)
return m[i][j];
if (i == j)
return 0;
int u = LookupChain(i + 1, j) + p[i] * p[i + 1] * p[j + 1];
s[i][j] = i;
for (int k = i + 1; k < j; k++) {
int t = LookupChain(i, k) + LookupChain(k + 1, j) + p[i] * p[k + 1] * p[j + 1];
if (t < u) {
u = t;
s[i][j] = k;
}
}
m[i][j] = u;
return u;
}
int MatrixChain::LookupChain()
{
return LookupChain(0, n - 1);
}
void MatrixChain::Output()
{ int i,j;
cout<<" m="<<endl;
cout<<" ";
for(j=0; j<n; j++)
if(j<2) cout<<setw(4)<<j;
else cout<<setw(6)<<j;
cout<<endl;
for(i=0; i<n; i++)
{ cout<<" "<<i<<" ";
for(j=0; j<n; j++)
{ if(i<j) cout<<setw(6)<<m[i][j];
else if(i==j) cout<<setw(2)<<m[i][j];
else cout<<setw(6)<<" ";
}
cout<<endl;
}
cout<<" s="<<endl;
cout<<" ";
for(j=0; j<n; j++) cout<<j<<" ";
cout<<endl;
for(i=0; i<n; i++)
{ cout<<" "<<i<<" ";
for(j=0; j<n; j++)
{ if(i<=j) cout<<s[i][j]<<" ";
else cout<<" ";
}
cout<<endl;
}
}
void main()
{ int nn=6,k;
int pp[7]={30,35,15,5,10,20,25};
MatrixChain mm(nn,pp);
cout<<"数据来源例7-5"<<endl;
cout<<endl<<"1.不用备忘录方法求矩阵连乘的数乘最少次数"<<endl;
k=mm.MChain(); cout<<" 最少数乘次数k="<<k<<endl;
mm.Traceback();
cout<<endl;
mm.Output();
cout<<endl<<"2.用备忘录方法求矩阵连乘的数乘最少次数"<<endl;
MatrixChain a(nn,pp);
k=a.LookupChain(); cout<<" 最少数乘次数k="<<k<<endl;
a.Traceback();
cout<<endl;
a.Output();
}
Thanks?(・ω・)ノ
|