@Alllll0235
2017-08-08T14:21:09.000000Z
字数 6689
阅读 1197
learning
Given a rod of length n inches and a table of prices for ,determine the maximum revenue obtainable by cutting up the rod and selling the prices.
| 长度i | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| 价格 | 1 | 5 | 8 | 9 | 10 | 17 | 17 | 20 | 24 | 30 |
maximum revenue
static int k=0;int cutrod(int p[],int n)//p为每个长度的价格,n为长度//q为输出,保存每个长度的最优价格{if(n==0)return 0;int q=0;for(int i=0;i<n;i++)q=MAX(q,p[i]+cutrod(p,n-i-1));k++;cout<<q<<" ";//输入每次调用函数保存的最优价格return q;}
输出结果:
enter the length of steer:4
1 5 1 8 1 5 1 10
the times of recursion:8
CUT-ROD calls itself recursively over and over again with the same parameter value; it solves the same subproblems repeatedly.
static int k=0;int memoized_cut_rod_aux(int p[],int n,int r[]){if (r[n]>=0)return r[n];int q;if (n==0)q=0;else{q=-1;for(int i=0;i<n;i++)q=max(q,p[i]+memoized_cut_rod_aux(p,n-i-1,r));}r[n]=q; //保存子问题的解k++; //计算递归次数cout<<q<<" ";return q;}int memoized_cut_rod(int p[],int n){int *r=new int [n+1];for(int i=0;i<=n;i++)r[i]=-1;return memoized_cut_rod_aux(p,n,r);}
输出结果:
enter the length of steer:4
0 1 5 8 10
the times of recursion:5
int bottom_up_cut_rod(int p[],int n){int *r=new int [n+1];r[0]=0;for(int j=1;j<=n;j++){int q=-1;for(int i=0;i<j;i++)q=max(q,p[i]+r[j-i-1]);r[j]=q;}return r[n];}
输出结果:
enter the length of steer:4
the maximum revenue:10
Given a sequence (chain) of n matrix to be multiplied, and we wish to compute the product .
void Matrix_Chain_Order(int* p,int **m,int** s){//矩阵链的长度int n = sizeof(p)/sizeof(p[0])-1;//①将对角线上的值先赋值为0for (int i = 1; i <= n; i++){m[i][i] = 0;}int l = 0; //l为矩阵链的长度//m[i][j]的第一个参数int i = 0;//m[i][j]的第二个参数int j = 0;//②以长度L为划分,L从2开始到nfor (l = 2; l <= n; l++){//循环第一个参数,因为l的长度至少为2,所以i和j在这个循环里面肯定不相等for (i = 1; i <= n - l + 1; i++){//因为j-i+1=l,所以j=l+i-1j = i + l - 1;//对于每个特定的i和j的组合,遍历此时所有的合适k值,k大于等于i小于jfor (int k = i ; k < j; k++){//这里k不能等于j,因为后面要m[k+1][j],不然k+1就比j大了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(int** s, 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 << ")";}}
Given two sequences and and wish to find maximumlength common subsequence of X and Y.
Step 3: Computing the length of an LCS
相当于
相当于
相当于
void LCSLength(int m,int n,char *x,char *y,int **c,int **b){//x和y是目标序列//m是x的长度,n是y的长度int i,j;//当x或y为空序列时,无公共子序列for(i=1; i<=m; i++) //x长度为i,y长度为jc[i][0] = 0;for(i=1; i<=n; i++)c[0][i] = 0;for(i=1; i<=m; i++){for(j=1; j<=n; 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,char *x,int **b){if(i==0 || j==0){return;}if(b[i][j]==1){LCS(i-1,j-1,x,b);cout<<x[i]<<" ";}else if(b[i][j]==2){LCS(i-1,j,x,b);}else{LCS(i,j-1,x,b);}}
int LCS(string sx, string sy){int sizex = sx.length();int sizey = sy.length();//创建动态二维数组int **lcs = new int*[sizex];for (int i = 0; i < sizex; i++){lcs[i] = new int[sizey];}//求解LCSint max = 0;for (int i = 0; i < sizex; i++){for (int j = 0; j < sizey; j++){//计算初始值:lcs[0][x]和lcs[x][0]的值if (i == 0 || j == 0){if (sx[i] == sy[j]){lcs[i][j] = 1;}else{if (i == 0 && j == 0)lcs[i][j] = 0;else if (i == 0 && j != 0)lcs[i][j] = lcs[i][j-1];elselcs[i][j] = lcs[i-1][j];}}else //其他lcs[i][j]的值{if (sx[i] == sy[j])lcs[i][j] = lcs[i-1][j-1] + 1;elselcs[i][j] = lcs[i-1][j] > lcs[i][j-1] ? lcs[i-1][j] : lcs[i][j-1];}if (lcs[i][j] > max)max = lcs[i][j];}}//释放动态二维数组for (int i = 0; i < sizex; ++i){delete[] lcs[i];}delete[] lcs;return max;}

| 0 | 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|---|
| 0.15 | 0.10 | 0.05 | 0.10 | 0.20 | ||
| 0.05 | 0.10 | 0.05 | 0.05 | 0.05 | 0.10 |
: key
: the probability of
: the dummy key which represents all values between and
: the probability of
Every search is either successful (finding some key ) or unsuccessful (findng some dummy key ).
E [Search cost in T}
copyvoid OPTIMAL_BST(int *p,int *q,int n,int **e,int **w,int **root){//初始化只包含虚拟键的子树for(int i=1;i<=n+1;i++){e[i][i-1]=q[i-1];w[i][i-1]=q[i-1];}for(int l=1;l<=n;l++){for(int i=1;i<=n-l+1;i++){j=i+l-1;e[i][j]=INT_MAX;w[i][j]=w[i][j-1]+p[i]+q[j]; //字数期望概率总和for(int r=i;r<=j;r++){int t=e[i][r-1]+e[r+1][j]+w[i][j];if(t<e[i][j]){e[i][j]=t; //字数期望代价root[i][j]=r; //记录根节点}}}}}