動態規劃相關問題源碼(包括矩陣鏈乘、LCS、和max sum)

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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章