http://codeforces.com/contest/552/problem/E

E. Vanya and Brackets
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Vanya is doing his maths homework. He has an expression of form ,
where x1, x2, ..., xn are
digits from 1 to 9, and
sign  represents
either a plus '+' or the multiplication sign '*'.
Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.

Input

The first line contains expression s (1 ≤ |s| ≤ 5001, |s| is
odd), its odd positions only contain digits from 1 to 9,
and even positions only contain signs  +  and  * .

The number of signs  *  doesn't exceed 15.

Output

In the first line print the maximum possible value of an expression.

Sample test(s)
input
3+5*7+8*4
output
303
input
2+3*5
output
25
input
3*4*5
output
60
Note

Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.

Note to the second sample test. (2 + 3) * 5 = 25.

Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60).

/**
CF552E 字符串 表达式求值
题目大意:给定一个字符串仅仅是做1~9之间的加法和乘法,如今在表达式中加上一对括号。问怎样加才干使表达式的值最大
解题思路:左括号必须在一个*的后面,右括号必须在一个*的前面,假设不是这样一定不是最优。 有了这个结论,分成三部分算一下就能够了
*/
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long LL;
char s[5005];
int n,a[105];
LL get(int l,int r)
{
LL ans=0,tmp=1;
for(int i=l;i<=r;i++)
{
if(isdigit(s[i]))
{
tmp*=s[i]-'0';
}
if(s[i]=='+')
{
ans+=tmp;
tmp=1;
}
}
ans+=tmp;
tmp=1;
LL sum=0;
for(int i=0;i<n;i++)
{
if(i==l)
{
tmp*=ans;
i=r;
}
else
{
if(isdigit(s[i]))
{
tmp*=s[i]-'0';
}
if(s[i]=='+')
{
sum+=tmp;
tmp=1;
}
}
}
sum+=tmp,tmp=1;
// printf(">>>%d %d %d %d\n",l,r,ans,sum);
return sum;
}
int main()
{
scanf("%s",s);
n=strlen(s);
int k=0;
a[k++]=0;
for(int i=0;i<n;i++)
{
if(s[i]=='*')
{
a[k++]=i-1;
a[k++]=i+1;
}
}
if(a[k-1]!=n-1)
a[k++]=n-1;
LL maxx=0;
for(int i=0;i<k;i++)
{
for(int j=i;j<k;j++)
{
LL t=get(a[i],a[j]);
maxx=max(maxx,t);
}
}
printf("%lld\n",maxx);
return 0;
}

CF552E 字符串 表达式求值的更多相关文章

  1. C++之字符串表达式求值

    关于字符串表达式求值,应该是程序猿们机试或者面试时候常见问题之一,昨天参加国内某IT的机试,压轴便为此题,今天抽空对其进行了研究. 算术表达式中最常见的表示法形式有 中缀.前缀和 后缀表示法.中缀表示 ...

  2. Java 计算数学表达式(字符串解析求值工具)

    Java字符串转换成算术表达式计算并输出结果,通过这个工具可以直接对字符串形式的算术表达式进行运算,并且使用非常简单. 这个工具中包含两个类 Calculator 和 ArithHelper Calc ...

  3. NYOJ 1272 表达式求值 第九届省赛 (字符串处理)

    title: 表达式求值 第九届省赛 nyoj 1272 tags: [栈,数据结构] 题目链接 描述 假设表达式定义为: 1. 一个十进制的正整数 X 是一个表达式. 2. 如果 X 和 Y 是 表 ...

  4. nyoj305_表达式求值

    表达式求值 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min ...

  5. 数据结构--栈的应用(表达式求值 nyoj 35)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35 题目: 表达式求值 时间限制:3000 ms | 内存限制:65535 KB描述 AC ...

  6. 【算法】E.W.Dijkstra算术表达式求值

    算术表达式求值 我们要学习的一个栈的用例同时也是展示泛型的应用的一个经典例子,就是用来计算算术表达式的值,例如 ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) ) 如果将4乘以5,把3 ...

  7. 【NYOJ-35】表达式求值——简单栈练习

    表达式求值 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min ...

  8. 表达式求值(河南省第四届ACM试题-C题)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定指定的一个由3种函数组成的表达式,计算其数值. [题目分析] 一开始以为是后缀表达式,后来抽了没想出来,最后用了递归的方法解 ...

  9. java实现算术表达式求值

    需要根据配置的表达式(例如:5+12*(3+5)/7.0)计算出相应的结果,因此使用java中的栈利用后缀表达式的方式实现该工具类. 后缀表达式就是将操作符放在操作数的后面展示的方式,例如:3+2 后 ...

随机推荐

  1. 基于visual Studio2013解决面试题之0208二叉搜索树后序遍历序列

     题目

  2. VC/MFC 在ListCtl 控件中随鼠标移动提示单元格信息

    BEGIN_MESSAGE_MAP(CTipListCtrl, CListCtrl) //{{AFX_MSG_MAP(CTipListCtrl) ON_WM_MOUSEMOVE() ON_WM_DES ...

  3. memcached vs MySQL Memory engine table 速度比较_XMPP Jabber即时通讯开发实践_百度空间

    memcached vs MySQL Memory engine table 速度比较_XMPP Jabber即时通讯开发实践_百度空间 memcached vs MySQL Memory engin ...

  4. Velocity教程-脚本语法详解(转)

    Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象. 当Veloci ...

  5. JavaScript(一基本语法)

    本篇博客是对js的一个基本的了解,对于没有js基础的同学来说应该是个入门的基本吧 javascript 是原型化继承来的面向对象的动态类型的区分大小写的客户端的脚本语言.主要目的是为了解决服务器语言, ...

  6. Swift - 选择框(UIPickerView)的用法

    1,选择框可以让用户以滑动的方式选择值.示例如下: 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 ...

  7. 状态压缩动态规划 -- 棋盘问题 POJ 1321

    一个 N * N 的棋盘上面,有些格子不能放,放置 M 的棋子, 每两个棋子不能在同一行或者同一列,问有多少种放法 DFS太慢,用SCR好点点 Python 仅仅有 22 行,事实上能够更短.可是得排 ...

  8. iotop 分析系统那些进程占用io资源

    iotop -b -o  -t  -qqq >> /tmp/iotop.log 1.直接yum安装,rh6的光盘里有包. yum install iotop   2.命令参数介绍   -o ...

  9. mysql update 有无索引对比

    <pre name="code" class="html">mysql> desc ProductInfo; +--------------- ...

  10. Codeforces Round #309 (Div. 2) C

    题意: 就是给出总共同拥有k种颜色.每种颜色有ki种,排列必须满足第i+1种的最后一种颜色必须在第i种最后一种颜色的后面,其它颜色任意.总共同拥有多少种排列点的方法. 分析: 如果d[i]表示前i种的 ...