-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
98 lines (97 loc) · 3.47 KB
/
solution.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
class Solution
{
public:
vector<int> diffWaysToCompute(string input)
{
vector<int> num; //数字
vector<char> sign; //操作符
int tempNumber=0;
int len=input.size();
for(int i=0;i<len;)
{
if(input[i]>='0'&&input[i]<='9') //数字
{
// 提取字符串里的数字
tempNumber=0;
while(i<len&&input[i]>='0'&&input[i]<='9')
{
tempNumber = tempNumber*10 + input[i]-'0';
i++;
}
num.push_back(tempNumber);
}
else if(input[i]==' ')
i++;
else
{
// 提取操作符
sign.push_back(input[i]);
i++;
}
}
int numSum = num.size();
vector<int> res;
vector<vector<multiset<int> > > temp;
multiset<int> tempSet;
vector<multiset<int> >temVec;
for(int i=0; i<numSum; ++i)
temVec.push_back(tempSet);
for(int i=0; i<numSum; ++i)
temp.push_back(temVec);
for(int i=0; i<numSum; ++i)
temp[i][i].insert(num[i]);
for(int i=1; i<numSum; ++i)
{
if(sign[i-1]=='+')
temp[i-1][i].insert(num[i-1]+num[i]);
else if(sign[i-1]=='-')
temp[i-1][i].insert(num[i-1]-num[i]);
else
temp[i-1][i].insert(num[i-1]*num[i]);
}
for(int i=2; i<numSum; ++i)
{
for(int j=0; j+i<numSum; ++j)
{
multiset<int>::iterator ite=temp[j][j].begin();
for(multiset<int>::iterator ite2=temp[j+1][i+j].begin(); ite2!=temp[j+1][i+j].end(); ++ite2)
{
if(sign[j]=='+')
temp[j][i+j].insert(*ite+(*ite2));
else if(sign[j]=='-')
temp[j][i+j].insert(*ite-(*ite2));
else
temp[j][i+j].insert(*ite*(*ite2));
}
ite=temp[i+j][i+j].begin();
for(multiset<int>::iterator ite2=temp[j][i+j-1].begin(); ite2!=temp[j][i+j-1].end(); ++ite2)
{
if(sign[i+j-1]=='+')
temp[j][i+j].insert(*ite2+(*ite));
else if(sign[j+i-1]=='-')
temp[j][i+j].insert(*ite2-(*ite));
else
temp[j][i+j].insert(*ite2*(*ite));
}
for(int k=j+1; k<i+j-1; ++k)
{
for(multiset<int>::iterator ite2=temp[j][k].begin(); ite2!=temp[j][k].end(); ++ite2)
{
for(multiset<int>::iterator ite3=temp[k+1][j+i].begin(); ite3!=temp[k+1][j+i].end(); ++ite3)
{
if(sign[k]=='+')
temp[j][i+j].insert(*ite2+(*ite3));
else if(sign[k]=='-')
temp[j][i+j].insert(*ite2-(*ite3));
else
temp[j][i+j].insert(*ite2*(*ite3));
}
}
}
}
}
for(multiset<int>::iterator ite2=temp[0][numSum-1].begin();ite2!=temp[0][numSum-1].end();++ite2)
res.push_back(*ite2);
return res;
}
};