标签:
•给定一个中序遍历为1,2,3,…,n的二叉树
1 #include <stdio.h> 2 #include <string.h> 3 4 int a[30]; 5 int dp[30][30];//dp[i][j] 表示某子树的中序遍历为i-->j 那么dp[i][i]则表示i为叶子结点 6 int root[30][30]; 7 8 void print(int i, int j) 9 { 10 if(i>j)return; 11 if(root[i][j]==-1) return; 12 printf("%d ",root[i][j]+1); 13 print(i,root[i][j]-1); 14 print(root[i][j]+1,j); 15 } 16 int main() 17 { 18 int n,i,j,k,t; 19 while(scanf("%d",&n)!=EOF) 20 { 21 memset(dp,-1,sizeof(dp)); 22 memset(root,-1,sizeof(root)); 23 for(i=0; i<n; ++i) 24 { 25 scanf("%d",&a[i]); 26 dp[i][i] = a[i]; 27 root[i][i] = i; 28 } 29 for(t=1; t<n; ++t)//枚举长度 30 for(i=0; i<n-t; ++i)//枚举起点 31 { 32 j = t + i;//区间总长度 33 for(k=i+1; k<j; ++k) 34 if(dp[i][j] < (dp[i][k-1]*dp[k+1][j]+a[k])) 35 { 36 dp[i][j] = dp[i][k-1]*dp[k+1][j]+a[k]; 37 root[i][j] = k; 38 } 39 if(dp[i][j] < dp[i][j-1] + a[j]) 40 { 41 dp[i][j] = dp[i][j-1] + a[j]; 42 root[i][j] = j; 43 } 44 if(dp[i][j] < dp[i+1][j] + a[i]) 45 { 46 dp[i][j] = dp[i+1][j] + a[i]; 47 root[i][j] = i; 48 } 49 } 50 printf("%d\n",dp[0][n-1]); 51 print(0,n-1); 52 } 53 }
标签:
原文地址:http://www.cnblogs.com/justPassBy/p/4293286.html