Call of Accepted【中缀表达式】2018ICPC沈阳网络赛

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 “ d d ”. “ x d y x   d   y ” means that an y y -sided dice should be rolled x x times and the sum of the results is taken.
Formally, for any two integers x,y x , y satisfying x0 x ≥ 0 and y1 y ≥ 1 , “ x d y x   d   y ” means the sum of x x random integers in the range of [1,y] [ 1 , y ] . 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. 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 ∈ [ 0 , + ∞ ) , b[1,+) b ∈ [ 1 , + ∞ )
这些随机数的和。运算优先级高于乘法

In particular, the precedence of “d” is above “*”.
输入一个字符串为表达式,求其最大值和最小值

#include
#include
#include
#include
#include
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; }
};
queuesuf;//后缀表达式
stack<char>st;//符号栈
stackmi, 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;//栈中最后剩下一个数即为所求
    }
}

你可能感兴趣的:(解题报告,Data,structure,数据结构题,模拟)