File tree 1 file changed +24
-2
lines changed
1 file changed +24
-2
lines changed Original file line number Diff line number Diff line change 1
1
#include < bits/stdc++.h>
2
+ #define N 10
2
3
using namespace std ;
3
4
5
+ int dp[N][N], parans[N][N];
6
+
7
+ void printParans (int i, int j) {
8
+ if (i+1 == j) {
9
+ cout<<char (' A' +i-1 )<<char (' A' +i);
10
+ return ;
11
+ }
12
+ if (i == j) {
13
+ cout<<char (' A' +i-1 );
14
+ return ;
15
+ }
16
+ cout<<' (' ;
17
+ printParans (i,parans[i][j]);
18
+ cout<<" )(" ;
19
+ printParans (parans[i][j]+1 ,j);
20
+ cout<<' )' ;
21
+ }
22
+
4
23
void matrixMult (int dimensions[], int n) {
5
- int dp[n][n];
6
24
int i,j,k,l;
7
25
8
26
for (i=1 ; i<n; i++) {
@@ -14,12 +32,16 @@ void matrixMult(int dimensions[], int n) {
14
32
dp[i][j] = INT_MAX;
15
33
for (k=i; k<=j-1 ; k++) {
16
34
int cost = dp[i][k] + dp[k+1 ][j] + dimensions[i-1 ]*dimensions[k]*dimensions[j];
17
- if (cost < dp[i][j])
35
+ if (cost < dp[i][j]) {
18
36
dp[i][j] = cost;
37
+ parans[i][j] = k;
38
+ }
19
39
}
20
40
}
21
41
}
22
42
cout<<" Min cost : " <<dp[1 ][n-1 ]<<endl;
43
+ printParans (1 ,n-1 );
44
+ cout<<endl;
23
45
}
24
46
25
47
int main () {
You can’t perform that action at this time.
0 commit comments