root[i][j]的值的为在状态转移方程中取最大值的那个k。
#include
#include
#include
#include
using namespace std;
#define N 32
#define MAX(a, b) ((a)>(b)?(a):(b))
int n, in_ord[N], f[N][N], root[N][N];
void pri_ord(int st, int en) {
int r = root[st][en];
cout<=st)
pri_ord(st, r-1);
if(r+1<=en)
pri_ord(r+1, en);
}
int main() {
ios::sync_with_stdio(false);
int i, j, k, gap, tmp, tmp1;
cin>>n;
for(i=1; i<=n; i++)
cin>>in_ord[i];
for(i=0; i<=n; i++) {
f[i][i] = in_ord[i];
f[i+1][i] = 1;
root[i][i] = i;
}
for(gap = 2; gap<=n; gap++)
for(i=1; j=i+gap-1, j<=n; i++) {
tmp = 0;
for(k=i; k<=j; k++) {
tmp1 = f[i][k-1]*f[k+1][j] + in_ord[k];
if(tmp1 > tmp) {
tmp = tmp1;
root[i][j] = k;
}
}
f[i][j] = tmp;
}
cout<