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 “ d d ”. “ xdy x d y ” means that an y y -sided dice should be rolled times and the sum of the results is taken.
Formally, for any two integers x,y x , y satisfying x≥0 x ≥ 0 and y≥1 y ≥ 1 , “ xdy x d y ” means the sum of x x random integers in the range of . It’s obvious that either x<0 x < 0 or y<1 y < 1 makes the expression x d y x d y illegal. For example: “ 2d6 2 d 6 ” 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. For example, an “ 1d100 1 d 100 ” can be used to determine whether an action with a percentage probability is successful, and a “ 3d6+3∗2 3 d 6 + 3 ∗ 2 ” can be used to calculate the total damage when being attacked by 3 monsters simultaneously. In particular, the precedence of “ d d ” is above “ ∗ ∗ ”. Since the operator “” does not satisfy the associative law, it’s necessary to make sure that “ d 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,那么只需要在算术表达式求值基础上改一改就可以了。
因为要求最大值和最小值,还需要记忆化一下。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[110];
ll d[110][110][2];
int v[110][110][2];
int A[110];
ll dfs(int L,int R,int tag)
{
if(s[L]=='('&&A[L]==R)return dfs(L+1,R-1,tag);
if(v[L][R][tag])return d[L][R][tag];
v[L][R][tag]=1; //记忆化
int cnt=0;
for(int i=R;i>=L;i--)//从右往左找
{
if(s[i]=='(')cnt++;
if(s[i]==')')cnt--;
if(s[i]=='+'&&cnt==0)//根据运算符优先级,先找到+,-
{
if(tag==0)return d[L][R][0]=dfs(L,i-1,0)+dfs(i+1,R,0);
if(tag==1)return d[L][R][1]=dfs(L,i-1,1)+dfs(i+1,R,1);
}
if(s[i]=='-'&&cnt==0)
{
if(tag==0)return d[L][R][0]=dfs(L,i-1,0)-dfs(i+1,R,1);
if(tag==1)return d[L][R][1]=dfs(L,i-1,1)-dfs(i+1,R,0);
}
}
cnt=0;
for(int i=R;i>=L;i--)
{
if(s[i]==')')cnt++;
if(s[i]=='(')cnt--;
if(s[i]=='*'&&cnt==0)//再找到*
{
if(tag==0)
{
d[L][R][0]=min(dfs(L,i-1,0)*dfs(i+1,R,0),dfs(L,i-1,1)*dfs(i+1,R,1));
d[L][R][0]=min(d[L][R][0],dfs(L,i-1,0)*dfs(i+1,R,1));
d[L][R][0]=min(d[L][R][0],dfs(L,i-1,1)*dfs(i+1,R,0));
}
else
{
d[L][R][1]=max(dfs(L,i-1,0)*dfs(i+1,R,0),dfs(L,i-1,1)*dfs(i+1,R,1));
d[L][R][1]=max(d[L][R][1],dfs(L,i-1,0)*dfs(i+1,R,1));
d[L][R][1]=max(d[L][R][1],dfs(L,i-1,1)*dfs(i+1,R,0));
}
return d[L][R][tag];
}
}
cnt=0;
for(int i=L;i<=R;i++)
{
if(s[i]=='(')cnt++;
if(s[i]==')')cnt--;
if(s[i]=='d'&&cnt==0)//最后找到d
{
if(tag==0)return d[L][R][0]=dfs(L,i-1,0);
if(tag==1)return d[L][R][1]=dfs(L,i-1,1)*dfs(i+1,R,1);
}
}
ll sum=0;
for(int i=L;i<=R;i++)sum=sum*10+s[i]-'0';//没有运算符
return d[L][R][tag]=sum;
}
stack<int>p;
int main()
{
while(scanf("%s",s+1)!=EOF)
{
memset(v,0,sizeof v);
int n=strlen(s+1);
for(int i=1;i<=n;i++)//预处理出每个'('对应的')'
{
if(s[i]=='(')p.push(i);
if(s[i]==')')
{
A[p.top()]=i;
p.pop();
}
}
printf("%lld %lld\n",dfs(1,n,0),dfs(1,n,1));
}
return 0;
}