1041: XX's easy problem

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 41  Solved: 7
[Submit][Status][Web
Board
]

Description

XX is a good student who likes to ask questions.But sometimes when you get the problem, you are not willing to answer his question bucause the problem is too easy.For example,He'll ask how much a + b is equal to, or how much a +
b is equal to,etc.
Today he just got a new problem,so he asks you for help:He has an expression of form x1 op x2 op x3.....xn,and op is just '+' or '*',xi(1 <= i <= n) a digits between 1 and 9。However, this problem is not so easy, you need 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,x1 op x2 op x3.....xn(1 <= n <= 50),op is '+' or '*'.The answer maxmized dosen't exceed 2^63-1.

Output

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

Sample Input

1+2*3
2*2*2

Sample Output

9
8

哎西每次可以AC的时候先给我的CE真是醉了,HUST这个OJ真是有毒,字符串长度的unsigned和普通int都要分那么清楚。这题怎么说呢,做了我一个晚上+半个晚上(估计是我思路不太好,只能向麻烦的做法靠近)...试了各种方法,最后特判+巨烦的多次string重定向终于过了。恭喜下自己终于上60题,可以够到大神远远跑在前面留下的灰尘了....做法嘛没什么好说的,毫无算法,枚举所有括号的位置,然后按照括号和加法乘法优先级进行模拟运算再取max。

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
string s;
inline LL fx(string s)//识别优先级的计算函数
{
LL i,cheng,tans;
for (i=0; i<(LL)s.size(); i++)
{
if(s[i]=='+')
s[i]=' ';
}
istringstream sin(s);
string temp;
LL t,sum=0;
while (sin>>temp)
{
for (i=0; i<(LL)temp.size(); i++)
{
if(temp[i]=='*')
temp[i]=' ';
}
istringstream ssin(temp);
tans=1;
while (ssin>>cheng)
{
tans=tans*cheng;
}
sum=sum+tans;
}
return sum;
}
inline string change(string s)//去括号并将括号内的数据进行计算并代回去
{
LL ans,len=(LL)s.size(),i,j,l=-1,r=-1;
string temp;
for (i=0; i<len; i++)
{
if(s[i]=='(')
{
l=i;
for (j=len-1; j>=i; j--)
{
if(s[j]==')')
{
r=j;
break;
}
}
break;
}
}
ans=fx(s.substr(l+1,r-l-1));
stringstream in;
in<<ans;
in>>temp;
s.replace(l,r-l+1,temp);
return s;
}
int main (void)
{
ios::sync_with_stdio(false);
LL i,j;
LL maxx,t;
string t1,t2,t3,t4;
while (getline(cin,s))
{
if(s.size()==1)
{
cout<<s<<endl;
continue;
}
LL len=s.size();
maxx=-9999999;
for (i=0; i+2<len; i+=2)//枚举所有括号位置(括号内至少两个数)
{
for (j=i+2; j<len; j+=2)
{
t3=s;
t1=s.substr(i,j-i+1);
t2=t3.erase(i,j-i+1);
t2.insert(i,'('+t1+')');
t4=change(t2);
t=fx(t4);
if(t>maxx)
{
maxx=t;
}
}
}
cout<<maxx<<endl;
}
return 0;
}

ACM程序设计选修课——1041: XX's easy problem(神烦的多次字符串重定向处理)的更多相关文章

  1. ACM程序设计选修课——Problem E:(ds:图)公路村村通(优先队列或sort+克鲁斯卡尔+并查集优化)

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  2. ACM程序设计选修课——Problem D: (ds:树)合并果子(最优二叉树赫夫曼算法)

    Problem D: (ds:树)合并果子 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 80  Solved: 4 [Submit][Status][ ...

  3. ACM程序设计选修课——Problem E:(ds:图)公路村村通(Prim)

    问题 E: (ds:图)公路村村通 时间限制: 1 Sec  内存限制: 128 MB 提交: 9  解决: 5 题目描述 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本, ...

  4. ACM程序设计选修课——Problem F:(ds:图)旅游规划(优先队列+SPFA)

    问题 F: (ds:图)旅游规划 时间限制: 1 Sec  内存限制: 128 MB 提交: 14  解决: 4 题目描述 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路 ...

  5. ACM程序设计选修课——1018: Common Subsequence(DP)

    问题 L: Common Subsequence 时间限制: 1 Sec  内存限制: 32 MB 提交: 70  解决: 40 [提交][状态][讨论版] 题目描述 A subsequence of ...

  6. ACM程序设计选修课——1043: Radical loves integer sequences(YY)

    1043: Radical loves integer sequences Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 36  Solved: 4 ...

  7. ACM程序设计选修课——1076汇编语言(重定向+模拟)

    1076: 汇编语言 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 34  Solved: 4 [Submit][Status][Web Board] ...

  8. ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)

    1057: Beautiful Garden Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 25  Solved: 12 [Submit][Statu ...

  9. ACM程序设计选修课——1065: Operations on Grids(暴力字符串)

    1065: Operations on Grids Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 17  Solved: 4 [Submit][Sta ...

随机推荐

  1. 通过Java代码实现图片的放大和缩小

    本文介绍的例子在Android安卓手机上测试通过. 先看看效果吧.可以看到这个开发好的安卓应用有三个按钮:Zoom In缩小图片,Zoom Out放大图片和Save保存. 初始页面: 可以在左边边框自 ...

  2. 将一个double类型的小数,按照四舍五入保留两位小数.

    package come.one01; public class One02 { public static void main(String[] args) { double numa = 3.14 ...

  3. alibaba druid监控页面的使用配置

    一.Maven中添加Durid连接池依赖 <!-- druid连接池 --> <dependency> <groupId>com.alibaba</group ...

  4. shell脚本,awk在需要的行上打打印空行。

    注解: 判断每行中是否包含字母a,包含了,就将$1的值赋值给变量a,然后判断变量a是否存在,存在打印一个空行,在将变量的值使用空变量b赋值,最后在打印输出. 结果就是在包含有字符a的行上打印一个空行.

  5. mysql grant 用户权限说明

    mysql grant 用户权限说明 Mysql 有多个个权限?经常记不住,今天总结一下,看后都能牢牢的记在心里啦!! 很明显总共28个权限:下面是具体的权限介绍:转载的,记录一下: 一.权限表 my ...

  6. iOS开发遇到的坑之六--使用cocopods管理第三方库时,编译出现Library not found for -lPods问题的解决办法

    在项目中有时候会遇到Library not found for -lPods(这里的IPods指的是你具体的第三方库)的问题 出现这个错误的原因是:xcode在编译的时候找不到这个库,从而导致项目无法 ...

  7. 使用 CFile 的子类 CStdioFile 的注意事项

    目前为止只用到了 ReadString,也了解了一下 WriteString. 由于程序需要,本来程序中是用的CFile, 但是需要逐行读取文件数据,所以谷歌找到了 ReadString 类 —— 继 ...

  8. GoF23种设计模式之结构型模式之代理模式

    一.概述 为其他对象提供一种代理以控制对这个对象的访问. 二.适用性 1.远程代理(RemoteProxy):为一个对象在不同的地址空间土工局部代表. 2.虚代理(VirtualProxy):根据需要 ...

  9. python并发编程之线程(创建线程,锁(死锁现象,递归锁),GIL锁)

    什么是线程 进程:资源分配单位 线程:cpu执行单位(实体),每一个py文件中就是一个进程,一个进程中至少有一个线程 线程的两种创建方式: 一 from threading import Thread ...

  10. ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study

    262144K   Ryuji is not a good student, and he doesn't want to study. But there are n books he should ...