110,535
社区成员
发帖
与我相关
我的任务
分享
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
using System;
using System.Numerics;
namespace CSharpTest
{
class Program
{
public static void Main()
{
int[] Items = new int[] { 8, -9, -1, 2, -7 };
BigInteger[, ,] Matrix = new BigInteger[Items.Length, Items.Length, 2];
for (int i = 0; i < Items.Length; i++)
{
for (int j = 0; j + i < Items.Length; j++)
{
int start = j;
int end = i + j;
if (i == 0)
Matrix[start, end, 0] = Matrix[start, end, 1] = Items[start];
else
{
Matrix[start, end, 0] = int.MinValue;
Matrix[start, end, 1] = int.MaxValue;
}
for (int k = start; k < end; k++)
{
Matrix[start, end, 0] = Max(Matrix[start, end, 0], Matrix[start, k, 0] + Matrix[k + 1, end, 0]);
Matrix[start, end, 0] = Max(Matrix[start, end, 0], Matrix[start, k, 0] * Matrix[k + 1, end, 0]);
Matrix[start, end, 0] = Max(Matrix[start, end, 0], Matrix[start, k, 1] * Matrix[k + 1, end, 1]);
Matrix[start, end, 1] = Min(Matrix[start, end, 1], Matrix[start, k, 1] + Matrix[k + 1, end, 1]);
Matrix[start, end, 1] = Min(Matrix[start, end, 1], Matrix[start, k, 0] * Matrix[k + 1, end, 1]);
Matrix[start, end, 1] = Min(Matrix[start, end, 1], Matrix[start, k, 1] * Matrix[k + 1, end, 0]);
}
}
}
Console.WriteLine(Matrix[0, Items.Length - 1, 0]);
Console.ReadKey();
}
public static BigInteger Max(BigInteger a, BigInteger b)
{
return a > b ? a : b;
}
public static BigInteger Min(BigInteger a, BigInteger b)
{
return a < b ? a : b;
}
}
}
void Test()
{
int i = Maximize("-2 * 1 + 1 * -1"); // i=4
}
class Node
{
public Node(int value, char op) {this.Value = value; this.Operator = op;}
public int Value {get; set;}
public char Operator {get; set;}
public Node More { get; set; }
public static Node operator &(Node n1, Node n2)
{
if (n1 == null || n2 == null) return null;
switch(n1.Operator)
{
case '+': return new Node(n1.Value + n2.Value, n2.Operator );
case '*': return new Node(n1.Value * n2.Value, n2.Operator );
case '-': return new Node(n1.Value - n2.Value, n2.Operator );
}
throw new InvalidOperationException(n1.Operator + " operator not supported");
}
}
int Maximize(string expression)
{
string[] toks = (expression + " /").Split(' ');
int operandCount = toks.Length / 2;
Node[,] matrix = new Node[operandCount, operandCount];
for (int i = 0; i < operandCount; i++)
{
matrix[i, i] = new Node(int.Parse(toks[i + i]), toks[i + i + 1][0]);
}
for (int e = 0; e < operandCount; e++)
{
for (int s = e - 1; s >= 0; s--)
{
Node max = new Node(int.MinValue, '/');
Node min = new Node(int.MaxValue, '/');
for (int split = e - 1; split >= s; split--)
{
Node[] ns =
{
matrix[s, split] & matrix[split + 1, e],
matrix[s, split].More & matrix[split + 1, e],
matrix[s, split] & matrix[split + 1, e].More,
matrix[s, split].More & matrix[split + 1, e].More,
};
foreach (Node node in ns)
{
if (node != null && node.Value > max.Value) max = node;
if (node != null && node.Value < min.Value) min = node;
}
}
matrix[s, e] = max; if(max.Value != min.Value) max.More = min;
}
}
return matrix[0, matrix.GetLength(0)-1].Value;
}