Call of Accepted

You and your friends are at the table, playing an old and interesting game - the Call of Cthulhu.
There is a mechanism in the game: rolling the dice. You use a notation to communicate the type of dice that needs to be rolled - the operator “dd”. “x d yx\ d\ y” means that an yy-sided dice should be rolled xx times and the sum of the results is taken.
Formally, for any two integers x,yx,y satisfying x0x\ge 0 and y1y\ge 1 , “x d yx\ d\ y” means the sum of xx random integers in the range of [1,y][1,y]. It’s obvious that either x<0x < 0 or y<1y<1 makes the expression x d yx\ d\ y illegal. For example: “2d62d6” means that rolling a 6-sided dice 2 times. At this time, the result can be at least [1,1]=2, and at most [6,6]=12. The results of rolling can be used extensively in many aspects of the game. In particular, the precedence of “d” is above “*”. Since the operator “d” does not satisfy the associative law, it’s necessary to make sure that “d” is right-associative.
Now the game has reached its most exciting stage. You are facing the Great Old One - Cthulhu. Because the spirit has been greatly affected, your sanity value needs to be deducted according to the result of rolling. If the sanity value loses too much, you will fall into madness. As a player, you want to write a program for knowing the minimum and maximum loss of sanity value before rolling, in order to make a psychological preparation.

The oldest and strongest emotion of mankind is fear, and the oldest and strongest kind of fear is fear of the unknown. ----H. P. Lovecraft

Input

There are multiple sets of input, at most 30 cases.

Each set of input contains a string of only ‘+’, ‘-’, ‘*’, ‘d’, ‘(’, ‘)’ and integers without spaces, indicating the expression of this sanity loss. The length of the expression will be at most 100.

It is guaranteed that all expressions are legal, all operators except for ‘(’ and ‘)’ are binary, and all intermediate results while calculating are integers in the range of [2147483648,2147483647][-2147483648, 2147483647].

The most merciful thing in the world, I think, is the inability of the human mind to correlate all its contents. We live on a placid island of ignorance in the midst of black seas of infinity, and it was not meant that we should voyage far. ----H. P. Lovecraft

Output

For each set of data, output a line of two integers separated by spaces, indicating the minimum and maximum possible values of the sanity loss.

样例输入

3d6*5
2d3d4

样例输出

15 90
2 24

题意:

定义一种运算d,a d b表示 [1,b][1,b]的范围内随机a次,a[0,+)a\in [0,+\infty),b[1,+)b\in[1,+\infty)
这些随机数的和。运算优先级高于乘法

In particular, the precedence of “d” is above “*”.

输入一个字符串为表达式,求其最大值和最小值

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
#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
struct ob {//为了能把符号和数字都放到一个队列里。。
ll nu;
char fu;
ob() {};
ob(ll x) { nu = x; fu = 0; }
ob(char c) { nu = 0; fu = c; }
};
queue<ob>suf;//后缀表达式
stack<char>st;//符号栈
stack<ll>mi, ma;//保存最大值和最小值
string s;
int pri[129];//符号优先级
int main()
{
pri['('] = 0;
pri['+'] = pri['-'] = 1;
pri['*'] = 2;
pri['d'] = 3;
pri[')'] = 4;//定义符号优先级
while (cin >> s)
{
while (suf.size()){suf.pop();}while (st.size()){st.pop();}while (mi.size()){mi.pop();}while (ma.size()){ma.pop();}
ll t = 0;
int i = 0;
while (i < s.size())
{
if (s[i] >= '0'&&s[i] <= '9')//数字直接入队
{
t = 0;
while (s[i] >= '0'&&s[i] <= '9')
{
t = t * 10 + s[i] - '0';
i++;
}
suf.push(ob(t));
}
else
{
if (st.empty())
{
st.push(s[i]);
}
else
{
if (s[i] == '(')
//左括号直接入栈
{
st.push(s[i]);
}
else if (s[i] == ')')
//遇到右括号,出栈直到左括号
{
while (st.top() != '(')
{
suf.push(ob(st.top()));
st.pop();
}
st.pop();
}
else if (pri[s[i]] > pri[st.top()])
//当前优先级大于栈顶符号,入栈
st.push(s[i]);
else if (pri[s[i]] < pri[st.top()])
//当前优先级小于栈顶符号,出栈直到栈顶比当前符号优先级小,入栈
{
while (st.size()&&pri[st.top()]>=pri[s[i]])
{
suf.push(ob(st.top()));
st.pop();
}
st.push(s[i]);
}
else if (pri[s[i]] == pri[st.top()])
//优先级相等,出栈一个,入栈一个
{
suf.push(ob(st.top()));
st.pop();
st.push(s[i]);
}
}//出栈就是输出到后缀表达式里
i++;
}
}
while (st.size())
{
suf.push(ob(st.top()));
st.pop();
}
while (suf.size())
{
ob oo = suf.front();
suf.pop();
if (oo.fu)
//遇到符号,拿出前两个数字,最大的两个数和最小的两个数互相操作得到四个结果
//取最大的和最小的分别放入最大值栈和最小值栈
{
ll a, b, A, B;
a = b = A = B = 0;
if (mi.size()) { a = mi.top(); mi.pop(); }
if (mi.size()) { b = mi.top(); mi.pop(); }
if (ma.size()) { A = ma.top(); ma.pop(); }
if (ma.size()) { B = ma.top(); ma.pop(); }
//b在前,a在后
ll res = 0;
if (oo.fu == 'd')
{
if (a < 1)a = 1;
if (A < 1)A = 1;
if (b < 0)b = 0;
if (B < 0)B = 0;
ma.push(max(a*b, max(a*B, max(A*b, A*B))));
mi.push(min(b, B));
}
else if (oo.fu == '+')
{
mi.push(min(a + b, min(a + B, min(A + b, A + B))));
ma.push(max(a + b, max(a + B, max(A + b, A + B))));
}
else if (oo.fu == '*')
{
mi.push(min(a * b, min(A * B, min(a*B, A*b))));
ma.push(max(A * B, max(a * b, max(a*B, A*b))));
}
else if (oo.fu == '-')
{
mi.push(min(B - A, min(b - a, min(B - a, b - A))));
ma.push(max(b - a, max(B - A, max(B - a, b - A))));
}
}
else//数字放到数字栈
{
mi.push(oo.nu);
ma.push(oo.nu);
}
}
cout << mi.top() << ' ' << ma.top() << endl;//栈中最后剩下一个数即为所求
}
}