Like a stone merge, in order to process the ring, we can copy the input data one after the other. In this way, the final result of the lifting point to find the largest.
Pay attention to the calculation of the price here, because our Data[i] only records the head of the bead and the tail of the beads is the head of the next bead.
Because it is necessary to calculate dp[i][j]
Used to dp[i][k] K is smaller than J so J order
Dp[k][j] K ratio i large so I reverse order
K Insert can be
for (int2*n-1; I >=1 ;--i) { for (int2*n ; + +J) {0; for (int k = i; k < J; + +k ) = Max (Dp[i][j], dp[i][k] + dp[k+1][j] + zz[i]*zz[k+1]*zz[j+1]);} }
Or it's easier to write with Len's way of traversing.
for(intLen =1; Len <2*n; ++Len) { for(inti =1; I <=2*n-len; ++i) {intj = i +Len; DP[I][J]=0; for(intK = i; K < J; ++k) Dp[i][j]= Max (Dp[i][j], dp[i][k]+dp[k+1][J] + zz[i]*zz[k+1]*zz[j+1]); }}
"Algorithmic Learning Notes" 85. Broken loop for chain sequence DP relaxation + cost SJTU OJ 1073 Energy Necklace