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 后 ...
随机推荐
- mmtests使用简介
1.简介 mmtests是一个可配置的测试套件,可以被MM开发者用来进行一个常规测试.理想情况下,它可以与LTP,xfstests等测试工具结合起来实现自动化测试. 2.软件组织 run-mmtest ...
- 医院API免费接口的公布
医院通网(http://hospital.yi18.net) 站点上站快两个月了,基本已经稳定,尽管还有非常多小bug,但还是不影响大局.抱着数据开放和共享的理念,医院大全API接口 (http:// ...
- jbpm部署流程定义到MySql报乱码解决方案
问题起因: 我在使用ant将流程定义和流程相关资源部署到JBPM数据库中的时候,报了下面一个错误. 错误提示,大概是: 11:33:40,781 ERROR JDBCExceptionReporter ...
- Substrings 第37届ACM/ICPC 杭州赛区现场赛C题(hdu 4455)
http://acm.hdu.edu.cn/showproblem.php?pid=4455 https://icpcarchive.ecs.baylor.edu/index.php?option=c ...
- Eclipse shift + ctrl + F 不好用
出现 shift + Ctrl + F 整理代码没有反应的情况,先检查下输入法是否是英文的,切换英文后再尝试.
- Php 解析XML文件
Php 解析XML文件 Php 解析XML文件,仅供学习參考!演示样例代码例如以下: <?php header("Content-type: text/html; charset=ut ...
- 使用Jmeter至WebService压力测试
使用Jmeter至WebService压力测试 目中我们使用了Jmeter对webservice进行了压力測试,Apache JMeter是Apache组织开发的基于Java的压力測试工具.用于对 ...
- Thinkphp学习04
原文:Thinkphp学习04 一.ThinkPHP 3 的输出 (重点) a.通过 echo 等PHP原生的输出方式在页面中输出 b.通过display方法输出 想分配变量可以使用as ...
- objective-c 中数据类型之六 数值类(NSValue)
// NSValue能够将c类型转换为Objective-C对象,如NSRange,CGPoint.CGSize,CGRect,CGVector,UIEdgeInsets,UIOffset NSRan ...
- pv操作 生产者消费者
#include <iostream> #include <stdlib.h> #include <pthread.h> #include <semaphor ...