您的位置:首页 > 其它

动态规划相关问题源码(包括矩阵链乘、LCS、和max sum)

2010-10-16 11:45 477 查看
functions.h

#include <iostream>

using namespace std;

#define SIZE 100 //数组大小

//****重载的计算数组长度函数****

int length(int a[]);

int length(char a[]);

//-----------------------------------

//****初始化数组函数******

void InitArray(int a[SIZE][SIZE]);

void InitArray(double a[SIZE][SIZE]);

//-----------------------------------

//****输入数组中数据*****

void Input(char p[]);

void Input(int p[]);

//-----------------------------------

//****打印数组中数据*****

void PrintArray(int a[SIZE][SIZE],int lr,int lc);

//-----------------------------------

//*****矩阵链乘*****

void Matrix_chain_order(double m[20][20],double s[20][20],int p[20]);

void Print_optimal_parens(double s[20][20],int i,int j);

//-----------------------------------

//********LCS********

void LCS_Length(char x[],char y[],int b[SIZE][SIZE],int c[SIZE][SIZE]);

void Print_LCS(int b[SIZE][SIZE],char x[],int i,int j);

//-----------------------------------

//*******Max_Sum******

int BetterMaxSum(int n, int a[], int &besti, int &bestj);

//-----------------------------------

functions.cpp

#include "Functions.h"

//**************************************************************

//----------计算数组a中元素个数,返回元素个数值------------------

int length(int a[])

{

int count=0;

while(a[count]!=0)

count++;

return count;

}

int length(char a[])

{

int count=0;

while(a[count]!='/0')

count++;

return count-1;

}

//--------------------------------------------------------------

//**************************************************************

//--------初始化二维数组a,将其中所有元素值均赋为0--------------

void InitArray(int a[SIZE][SIZE])

{

for(int i=0;i<SIZE;i++)

{

for(int j=0;j<SIZE;j++)

{

a[i][j]=0;

}

}

}

void InitArray(double a[SIZE][SIZE])

{

for(int i=0;i<SIZE;i++)

{

for(int j=0;j<SIZE;j++)

{

a[i][j]=0;

}

}

}

//--------------------------------------------------------------

//**************************************************************

//------------------输入数组p中元素-----------------------------

void Input(char p[])

{

cout<<"输入序列:";

gets(p);

//_____________________________

// 将字符数组中所有元素后移一位

//-----------------------------

int pos=0;

while(p[pos]!='/0')

pos++;

p[pos+2]='/0';

while(pos>=0)

{

p[pos+1]=p[pos];

pos--;

}

//------------------------------

}

void Input(int p[])

{

int len;

cout<<"输入序列长度:";

cin>>len;

for(int i=0;i<len;i++)

{

cout<<"输入数据p"<<i<<":";

cin>>p[i];

}

p[len]=0;

}

//--------------------------------------------------------------

//**************************************************************

//--------------------输出数组中元素----------------------------

void PrintArray(int a[SIZE][SIZE],int lr,int lc)

{

for(int i=0;i<lr;i++)

{

for(int j=0;j<lc;j++)

cout<<a[i][j]<<" ";

cout<<endl;

}

cout<<endl;

}

//--------------------------------------------------------------

//**************************************************************

//-----------------矩阵链乘相关函数实现-------------------------

void Matrix_chain_order(double m[20][20],double s[20][20],int p[20])

{

int n=length(p)-1;

for(int i=1;i<=n;i++)

m[i][i]=0;

for(int l=2;l<=n;l++)

{

for(int i=1;i<=(n-l+1);i++)

{

int j=i+l-1;

m[i][j]=10000000;

for(int k=i;k<=(j-1);k++)

{

int q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];

if(q<m[i][j])

{

m[i][j]=q;

s[i][j]=k;

}

}

}

}

}

void Print_optimal_parens(double s[20][20],int i,int j)

{

if(i==j)

cout<<"A"<<i;

else

{

cout<<"(";

Print_optimal_parens(s,i,s[i][j]);

Print_optimal_parens(s,s[i][j]+1,j);

cout<<")";

}

}

//--------------------------------------------------------------

//**************************************************************

//-------------------LCS相关函数实现----------------------------

void LCS_Length(char x[],char y[],int b[SIZE][SIZE],int c[SIZE][SIZE])

{

int m=length(x);

int n=length(y);

for(int i=1;i<=m;i++)

c[i][0]=0;

for(int j=0;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;

b[i][j]=2;

}

else

{

if(c[i-1][j]>=c[i][j-1])

{

c[i][j]=c[i-1][j];

b[i][j]=3;

}

else

{

c[i][j]=c[i][j-1];

b[i][j]=4;

}

}

}

}

}

void Print_LCS(int b[SIZE][SIZE],char x[],int i,int j)

{

if(i==0||j==0)

return;

if(b[i][j]==2)

{

Print_LCS(b,x,i-1,j-1);

cout<<x[i];

}

else

{

if(b[i][j]==3)

Print_LCS(b,x,i-1,j);

else

Print_LCS(b,x,i,j-1);

}

}

//--------------------------------------------------------------

//**************************************************************

//---------------Max-Sum相关函数实现----------------------------

int BetterMaxSum(int n, int a[], int &besti, int &bestj)

{

int sum = 0;

for(int i=1; i <= n; ++i)

{

int thissum = 0;

for(int j=i; j <= n; ++j)

{

thissum += a[j];

if(thissum > sum)

{

sum = thissum;

besti = i, bestj = j;

}

}

}

return sum;

}

//--------------------------------------------------------------

main.cpp

#include "Functions.h"

int main()

{

//######################################

//===========矩阵链乘测试代码=========

/*int p[20];

Input(p);

double m[20][20],s[20][20];

for(int i=0;i<20;i++)

{

for(int j=0;j<20;j++)

{

m[i][j]=0;

s[i][j]=0;

}

}

Matrix_chain_order(m,s,p);

Print_optimal_parens(s,1,length(p)-1);

*/

//########################################

//#######################################

//=========== LCS测试代码 ============

/*char x[SIZE],y[SIZE];

cout<<"请输入序列1:"<<endl;

Input(x);

cout<<"请输入序列2:"<<endl;

Input(y);

int b[SIZE][SIZE],c[SIZE][SIZE];

InitArray(b);

InitArray(c);

LCS_Length(x,y,b,c);

PrintArray(c,length(x)+1,length(y)+1);

PrintArray(b,length(x)+1,length(y)+1);

Print_LCS(b,x,length(x),length(y));*/

//#######################################

//#######################################

//==========Max-Sum测试代码==========

int a[20];

Input(a);

int besti,bestj;

int sum=BetterMaxSum(length(a),a,besti,bestj);

cout<<"Ans: i="<<besti<<" j="<<bestj;

cout<<", Max Sum is "<<sum<<endl;

//#######################################

system("PAUSE");

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: