//Made by syx
//Time : 2010年7月17日 13:58:47
//矩阵连乘
//最长公共子序列
//
//
//
//
/*
//最长公共子序列
#include <stdio.h>
char x[8] = {' ','A','B','C','B','D','A','B'};
char y[7] = {' ','B','D','C','A','B','A'};
int b[9][8] = {0};
int c[9][8] = {0};
void printArray(int _i1,int _j1,int _i2,int _j2,int (*m)[8]);//声明二维数组时要指定构成二维数组中的一维的长度
void lcsLength()
{
int m = 8;
int n = 7;
int i = 0,j = 0;
for(i=1; i<=m; ++i)
for(j=1; j<=m; ++j)
{
if(x[i]==y[j])
{
c[i][j] = c[i-1][j-1] + 1;
b[i][j] = 1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j] = c[i-1][j];
b[i][j] = 2;
}
else
{
c[i][j] = c[i][j-1];
b[i][j] = 3;
}
}
}
void lcs(int i,int j)
{
if(i==0 || j==0) return ;
if(b[i][j] == 1)
{
lcs(i-1,j-1);
printf("%c ",x[i]);
}
else if(b[i][j] == 2) lcs(i-1,j);
else lcs(i,j-1);
}
int main()
{
lcsLength();
lcs(7,6);
printf("\n");
printf("b\n");
printArray(0,9,0,8,b);
printf("c\n");
printArray(0,8,0,8,c);
return 0;
}
void printArray(int _i1,int _j1,int _i2,int _j2,int (*m)[8])
{
int i1 = _i1,i2 = _i2,j1 = _j1,j2 = _j2;
int i = 0 ,j = 0;
for(i=i1; i<j1; ++i)
{
for(j=i2; j<j2;++j)
printf("%3d",m[i][j]);
printf("\n");
}
printf("\n");
}
*/
//矩阵连乘问题
/*
//2.递归
#include <stdio.h>
//为简化,以书上6个矩阵为例
int p[7] = {30,35,15,5,10,20,25};
int m[7][7] = {0};
int s[7][7] = {0};
int reurMatrixChain(int l,int r)
{
if(m[l][r]>0) return m[l][r];
if(l==r) m[l][r] = 0;
else
{
int k = 0,t = 0;
s[l][r] = l;
m[l][r] = reurMatrixChain(l,l) + reurMatrixChain(l+1,r) + p[l-1]*p[l]*p[r];
for(k=l+1; k<r; ++k)
{
t = reurMatrixChain(l,k) + reurMatrixChain(k+1,r) + p[l-1]*p[k]*p[r];
if(t<m[l][r])
{
m[l][r] = t;
s[l][r] = k;
}
}
}
return m[l][r];
}
int main()
{
reurMatrixChain(1,6);
printf("\n");
int i,j;
for(i=1; i<7; ++i)
{
for(j=1; j<7;++j)
printf("%7d",m[i][j]);
printf("\n");
}
printf("\n\n");
for(i=1; i<7; ++i)
{
for(j=1; j<7;++j)
printf("%7d",s[i][j]);
printf("\n");
}
printf("\n\n");
return 0;
}
*/
//1.非递归动态规划
/*
#include <stdio.h>
//为简化,以书上6个矩阵为例
int p[7] = {30,35,15,5,10,20,25};
int m[7][7] = {0};
int s[7][7] = {0};
void matrixChain()
{
int n = 6;
int r = 0,i = 0,j = 0,k = 0,t = 0;
for(r=1; r<n; ++r)//1..5
for(i=1; i<n-r+1; ++i)//1..5
{
j = i + r;
m[i][j] = m[i][i] + m[i+1][j] + p[i-1]*p[i]*p[j];
s[i][j] = i;
for(k=i+1; k<j; ++k)
{
t = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
if(t<m[i][j])
{
m[i][j] = t;
s[i][j] = k;
}
}
}
}
void traceback(int i,int j)
{
if(i==j) return ;
traceback(i,s[i][j]);
traceback(s[i][j]+1,j);
printf("(A%d,A%d)",i,j);
}
int main()
{
matrixChain();
traceback(1,6);
printf("\n");
int i,j;
for(i=1; i<7; ++i)
{
for(j=1; j<7;++j)
printf("%7d",m[i][j]);
printf("\n");
}
printf("\n\n");
for(i=1; i<7; ++i)
{
for(j=1; j<7;++j)
printf("%7d",s[i][j]);
printf("\n");
}
printf("\n\n");
return 0;
}
*/