日期:2014-05-20  浏览次数:20834 次

【C# 每日一题7】求最大的结果
C# code

Description

Give you a expression,you can add some parenthesis to maximize result.Example as following:
1 + 2 * 3
Its result is 7 in the example.But you can add a parenthesis like (1 + 2) * 3,then the result is 9,and so it is the maximization of the example.You job is to find the maximizaton of a expression. To simplify this problem,you can assume that expression contains only operator + and *,but note that integers may be negative.

Input

Input contains multiple test cases.Each case begin with a integer N(2<=N<=100), the number of integers of a expression.Then follows the expression.Integers and operator spearate with a single whitespace.

Output

For each case print a single line with the maximized value of the expression.

Sample Input

4
1 + 2 * 3 + -1

Sample Output

8



解释一下,输入一个数n代表有下面算式里面有n个数字,每个数字中间有一个符号(+ or * )
之后输入这个算式
在随意加括号之后求出算式可以得到的最大值!
如:
4
1 + 2 * 3 + -1
变为( 1 + 2 ) * 3 + -1 = 8 
输出最大的值!!

求实现!哈哈!


------解决方案--------------------
类似24点?
------解决方案--------------------
如果没有负数,把所有加号都括起来就最大了吧
------解决方案--------------------
哦,n^3的DP可以,再想想有没有更好的方法。
------解决方案--------------------
这个需要看有多少个负数~有多少个纯小数~来决定是用+还是*
------解决方案--------------------
1 + 2 * 3 -1
括号所在位置,得到的值
0,2 =8
0,3 =6
1,3 =6
1,4 =6
2,4 =5
如果只能有1对括号,貌似循环是最直接的办法,尽管效率很不好。
------解决方案--------------------
1 + 2 * 3 -1
括号所在位置,得到的值
0,2 =8
0,3 =6
1,3 =6
1,4 =6
2,4 =5

------解决方案--------------------
碰到乘号就加括号吧,如果负数在最后就不管,负数在中间的情况考虑不出来,等待牛人....
------解决方案--------------------
下面用动态规划,空间复杂度为O(N^2),时间复杂度为O(N^3):
C# code
void Test
{
    int i = Maximize("1 + 2 * 3 + -1"); //i=8
}

class Node
{
    public int Value {get; set;}
    public char Operator {get; set;}
    public static Node operator &(Node n1, Node n2)
    {
        int result = 0;
        switch(n1.Operator)
        {
            case '+': result = n1.Value + n2.Value; break;
            case '*': result = n1.Value * n2.Value; break;
        }
        return new Node() { Value = result, Operator = n2.Operator };
    }
}

int Maximize(string expression)
{
    List<Node> nodes = new List<Node>();
    int lastValue = 0;
    foreach (string s in expression.Split(' '))
    {
        if ("+-/*".Contains(s))
        {
            nodes.Add(new Node() { Value = lastValue, Operator = s[0]});
        }
        else
        {
            lastValue = int.Parse(s, System.Globalization.CultureInfo.InvariantCulture);
        }
    }
    nodes.Add(new Node() { Value = lastValue, Operator = '+' });

    Node[,] matrix = new Node[nodes.Count, nodes.Count];
    for (int e = 0; e < matrix.GetLength(0); e++)
    {
        matrix[e, e] = nodes[e];
        for (int s = e - 1; s >= 0; s--)
        {
            Node max = new Node(){Value = int.MinValue};
            for (int split = e - 1; split >= s; split--)
            {
                Node node = matrix[s, split] & matrix[split + 1, e];
                if (node.Value > max.Value) max = node;
            }
            matrix[s, e] = max;
        }
    }
    return matrix[0, matrix.GetLength(0)-1].Value;
}

------解决方案--------------------