-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patha2.cpp
More file actions
174 lines (163 loc) · 3.07 KB
/
a2.cpp
File metadata and controls
174 lines (163 loc) · 3.07 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdio.h>
#include <string.h>
#include <bits/stdc++.h>
using namespace std;
vector <string> findallexpr(int n)
{
vector <vector <string> > expr;
for(int i=0;i<n;i++)
{
vector <string> vec;
if(i==0)
{
//base condition of dp
string s="1";
vec.push_back(s);
}
else
{
int m=i+1;
//additions
for(int j=1;j<=m/2;j++)
{
for(int k=0;k<expr[j-1].size();k++)
{
for(int l=0;l<expr[m-j-1].size();l++)
{
string s1 = expr[j-1][k];
string s2 = expr[m-j-1][l];
string s = s1 + "+" + s2;
int flag=0;
//check if duplicate
for(int a=0; a<vec.size();a++)
{
if(s==vec[a])
{
flag=1;
break;
}
}
//if not, add to expressions
if(!flag)
vec.push_back(s);
}
}
}
//multiplications
for(int j=2;j<=sqrt(m);j++)
{
if(m%j==0)
{
for(int k=0;k<expr[j-1].size();k++)
{
for(int l=0;l<expr[m/j-1].size();l++)
{
string s1 = expr[j-1][k];
int flag1=0; int flag2=0;
//to check if adding brackets is necessary
for(int a=0;a<s1.length();a++)
{
if(flag2==0&&s1[a]=='+')
flag1=1;
if(s1[a]=='(')
flag2++;
if(s1[a]==')')
flag2--;
}
if(flag1)
s1 = "(" + s1 + ")";
string s2 = expr[m/j-1][l];
flag1=0; flag2=0;
//to check if adding brackets is necessary
for(int a=0;a<s2.length();a++)
{
if(flag2==0&&s2[a]=='+')
flag1=1;
if(s2[a]=='(')
flag2++;
if(s2[a]==')')
flag2--;
}
if(flag1)
s2 = "(" + s2 + ")";
//add multiplication of both strings
string s = s1+"*"+s2;
int flag=0;
//check if duplicate
for(int a=0; a<vec.size();a++)
{
if(s==vec[a])
{
flag=1;
break;
}
}
//if not, add to vector
if(!flag)
vec.push_back(s);
}
}
}
}
}
expr.push_back(vec);
}
return expr[n-1];
}
void printallexpr(vector <string> vec, int n)
{
//appropriate printing formatting
if(n<10)
cout << n << " = ";
else
cout << n << " = ";
for(int i=0;i<vec.size();i++)
{
if(i==0)
cout << vec[i] << endl;
else
cout << " = "<<vec[i]<<endl;
}
cout << "....."<<vec.size()<<" expressions\n";
}
int main()
{
cout << "n = ";
int n;
cin >> n;
vector <string> vec = findallexpr(n);
cout << "+++ Before sorting\n";
printallexpr(vec,n);
//selection sorting done to vector
for(int i=0;i<vec.size()-1;i++)
{
string s=vec[i];
int count=0;
int index=i;
for(int k=0;k<s.length();k++)
{
if(s[k]=='1')
count++;
}
for(int j=i;j<vec.size();j++)
{
int count1=0;
for(int k=0;k<vec[j].length();k++)
{
if(vec[j][k]=='1')
count1++;
}
if(count1>count||(count==count1&&vec[j].length()>vec[index].length()))
{
count=count1;
index=j;
}
}
string temp=vec[index];
vec[index]=vec[i];
vec[i]=temp;
}
cout <<endl;
cout << "+++ After sorting\n";
printallexpr(vec,n);
}