-
Notifications
You must be signed in to change notification settings - Fork 0
/
Output Window.cpp
125 lines (115 loc) · 3.09 KB
/
Output Window.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <bits/stdc++.h>
using namespace std;
// User function Template for C++
class Solution{
public:
string matrixChainOrder(int p[], int n){
// Create a 2D array to store the minimum number of multiplications
vector<vector<int>> m(n, vector<int>(n, 0));
// Initialize the base cases
for (int i = 1; i < n; i++) {
m[i][i] = 0;
}
// Fill the matrix using bottom-up dynamic programming
for (int L = 2; L < n; L++) {
for (int i = 1; i < n - L + 1; i++) {
int j = i + L - 1;
m[i][j] = INT_MAX;
for (int k = i; k <= j - 1; k++) {
int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (q < m[i][j]) {
m[i][j] = q;
}
}
}
}
// Generate the parenthesis expression
string result = generateParenthesis(m, p, 1, n - 1);
return result;
}
string generateParenthesis(vector<vector<int>>& m, int p[], int i, int j) {
if (i == j) {
return string(1, 'A' + i - 1);
}
int minMultiplications = m[i][j];
for (int k = i; k < j; k++) {
int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (q == minMultiplications) {
string left = generateParenthesis(m, p, i, k);
string right = generateParenthesis(m, p, k + 1, j);
return "(" + left + right + ")";
}
}
return "";
}
};
int get(int p[],int n)
{
int m[n][n],ans = 1e9;
for (int i = 1; i < n; i++)
m[i][i] = 0;
for (int L = 2; L < n; L++) {
for (int i = 1; i < n-L+1; i++){
m[i][i + L - 1] = INT_MAX;
for (int k = i; k <= i + L - 2; k++){
int q = m[i][k] + m[k + 1][i + L - 1] + p[i-1] * p[k] * p[i + L - 1];
if (q < m[i][i + L - 1])
{
m[i][i + L - 1] = q;
}
}
}
}
return m[1][n-1];
}
int find(string s,int p[])
{
vector<pair<int,int>> arr;
int ans = 0;
for(auto t:s)
{
if(t=='(')
{
arr.push_back({-1,-1});
}
else if(t==')')
{
pair<int,int> b=arr.back();
arr.pop_back();
pair<int,int> a=arr.back();
arr.pop_back();
arr.pop_back();
arr.push_back({a.first,b.second});
ans += a.first*a.second*b.second;
}
else
{
arr.push_back({p[int(t-'A')],p[int(t-'A')+1]});
}
}
return ans;
}
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;
string result = ob.matrixChainOrder(p, n);
if(find(result,p)==get(p,n))
{
cout<<"True"<<endl;
}
else
{
cout<<"False"<<endl;
}
}
return 0;
}