-
Notifications
You must be signed in to change notification settings - Fork 1
/
29-brackets-in-matrix-chain-multiplication.cpp
77 lines (65 loc) · 1.82 KB
/
29-brackets-in-matrix-chain-multiplication.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution{
public:
void getParenthesis(vector<vector<int>>& bracket, int i, int j, int n, char& name, string& ans)
{
if(i > j || i >= n || j >= n)
return;
if(i == j)
{
ans += name;
name++;
return;
}
ans += "(";
getParenthesis(bracket, i, bracket[i][j], n, name, ans);
getParenthesis(bracket, bracket[i][j] + 1, j, n, name, ans);
ans += ")";
}
string matrixChainOrder(int p[], int n){
vector<vector<int>> dp(n, vector<int>(n));
vector<vector<int>> bracket(n, vector<int>(n));
string ans = "";
char name = 'A';
for(int i = 0; i < n; ++i)
dp[i][i] = 0;
for(int gap = 2; gap < n; ++gap)
{
for(int i = 0; i <= (n - gap); ++i)
{
int j = i + gap - 1;
dp[i][j] = INT_MAX;
for(int k = i; k < j; ++k)
{
int cost = dp[i][k] + dp[k + 1][j] + p[i - 1] * p[k] * p[j];
if(cost < dp[i][j])
{
dp[i][j] = cost;
bracket[i][j] = k;
}
}
}
}
getParenthesis(bracket, 1, n - 1, n, name, ans);
return ans;
}
};
// { Driver Code Starts.
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int p[n];
for(int i = 0;i < n;i++)
cin>>p[i];
Solution ob;
cout<<ob.matrixChainOrder(p, n)<<"\n";
}
return 0;
} // } Driver Code Ends