CF552E 字符串 表达式求值
http://codeforces.com/contest/552/problem/E
1 second
256 megabytes
standard input
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.
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.
In the first line print the maximum possible value of an expression.
3+5*7+8*4
303
2+3*5
25
3*4*5
60
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 字符串 表达式求值的更多相关文章
- C++之字符串表达式求值
关于字符串表达式求值,应该是程序猿们机试或者面试时候常见问题之一,昨天参加国内某IT的机试,压轴便为此题,今天抽空对其进行了研究. 算术表达式中最常见的表示法形式有 中缀.前缀和 后缀表示法.中缀表示 ...
- Java 计算数学表达式(字符串解析求值工具)
Java字符串转换成算术表达式计算并输出结果,通过这个工具可以直接对字符串形式的算术表达式进行运算,并且使用非常简单. 这个工具中包含两个类 Calculator 和 ArithHelper Calc ...
- NYOJ 1272 表达式求值 第九届省赛 (字符串处理)
title: 表达式求值 第九届省赛 nyoj 1272 tags: [栈,数据结构] 题目链接 描述 假设表达式定义为: 1. 一个十进制的正整数 X 是一个表达式. 2. 如果 X 和 Y 是 表 ...
- nyoj305_表达式求值
表达式求值 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min ...
- 数据结构--栈的应用(表达式求值 nyoj 35)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35 题目: 表达式求值 时间限制:3000 ms | 内存限制:65535 KB描述 AC ...
- 【算法】E.W.Dijkstra算术表达式求值
算术表达式求值 我们要学习的一个栈的用例同时也是展示泛型的应用的一个经典例子,就是用来计算算术表达式的值,例如 ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) ) 如果将4乘以5,把3 ...
- 【NYOJ-35】表达式求值——简单栈练习
表达式求值 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min ...
- 表达式求值(河南省第四届ACM试题-C题)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定指定的一个由3种函数组成的表达式,计算其数值. [题目分析] 一开始以为是后缀表达式,后来抽了没想出来,最后用了递归的方法解 ...
- java实现算术表达式求值
需要根据配置的表达式(例如:5+12*(3+5)/7.0)计算出相应的结果,因此使用java中的栈利用后缀表达式的方式实现该工具类. 后缀表达式就是将操作符放在操作数的后面展示的方式,例如:3+2 后 ...
随机推荐
- session对象和applicatione对象
ASP.NET 的常用对象有:response对象.request对象.application对象.server对象.session对象.下面主要讨论session对象和cookie对象. sessi ...
- MFC的消息机制
MFC的消息循环(::GetMessage,::PeekMessage)消息泵(CWinThread::PumpMessage)和MFC的消息在窗口之间的路由是两件不同的事情 分两个步骤完成: 1 “ ...
- c++ :: 域操作符
c++ :: 域操作符 作用域:变量在程序中的起作用范围简单分为:全局作用域,局部作用域,语句作用域作用域优先级:范围越小优先级越高作用域运算符:"::" 如果希望在局部变量的作用 ...
- Dom4j和Xpath(转)
1.DOM4J简介 DOM4J是 dom4j.org 出品的一个开源 XML 解析包.DOM4J应用于 Java 平台,采用了 Java 集合框架并完全支持 DOM,SAX 和JAXP. DOM4J使 ...
- 使用Boost库中的组件进行C++内存管理
C++标准库中的auto_ptr,智能指针,部分的解决了获取资源自动释放的问题 在Boost中,提供了6中智能指针:scoped_ptr, scoped_array, shared_ptr, shar ...
- HDU 2112 HDU Today(Dijkstra)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others ...
- hdu 3832 Earth Hour (最短路变形)
Earth Hour Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Tota ...
- opencv 训练自己的分类器汇总
原地址:http://www.cnblogs.com/zengqs/archive/2009/02/12/1389208.html OpenCV训练分类器 OpenCV训练分类器 一.简介 目标检测方 ...
- python - ImportError: No module named http.cookies error when installing cherrypy 3.2 - Stack Overflow
python - ImportError: No module named http.cookies error when installing cherrypy 3.2 - Stack Overfl ...
- android4.3环境搭建
方案一: 首先android环境搭建有如下几个东西是必须准备的: 1. Eclipse (下载地址:http://www.eclipse.org/downloads/,建议至少3.4及以上版本) 2 ...