ACM程序设计选修课——1041: XX's easy problem(神烦的多次字符串重定向处理)
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(神烦的多次字符串重定向处理)的更多相关文章
- ACM程序设计选修课——Problem E:(ds:图)公路村村通(优先队列或sort+克鲁斯卡尔+并查集优化)
		畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ... 
- ACM程序设计选修课——Problem D: (ds:树)合并果子(最优二叉树赫夫曼算法)
		Problem D: (ds:树)合并果子 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 80 Solved: 4 [Submit][Status][ ... 
- ACM程序设计选修课——Problem E:(ds:图)公路村村通(Prim)
		问题 E: (ds:图)公路村村通 时间限制: 1 Sec 内存限制: 128 MB 提交: 9 解决: 5 题目描述 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本, ... 
- ACM程序设计选修课——Problem F:(ds:图)旅游规划(优先队列+SPFA)
		问题 F: (ds:图)旅游规划 时间限制: 1 Sec 内存限制: 128 MB 提交: 14 解决: 4 题目描述 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路 ... 
- ACM程序设计选修课——1018: Common Subsequence(DP)
		问题 L: Common Subsequence 时间限制: 1 Sec 内存限制: 32 MB 提交: 70 解决: 40 [提交][状态][讨论版] 题目描述 A subsequence of ... 
- ACM程序设计选修课——1043: Radical loves integer sequences(YY)
		1043: Radical loves integer sequences Time Limit: 1 Sec Memory Limit: 128 MB Submit: 36 Solved: 4 ... 
- ACM程序设计选修课——1076汇编语言(重定向+模拟)
		1076: 汇编语言 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 34 Solved: 4 [Submit][Status][Web Board] ... 
- ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)
		1057: Beautiful Garden Time Limit: 5 Sec Memory Limit: 128 MB Submit: 25 Solved: 12 [Submit][Statu ... 
- ACM程序设计选修课——1065: Operations on Grids(暴力字符串)
		1065: Operations on Grids Time Limit: 3 Sec Memory Limit: 128 MB Submit: 17 Solved: 4 [Submit][Sta ... 
随机推荐
- MovieReview—Coco(寻梦环游记)
			Dream & Family The protagonist in the movie is hard to choose between family and dream. ... 
- Java的引用StrongReference、 SoftReference、 WeakReference 、PhantomReference
			1. Strong Reference StrongReference 是 Java 的默认引用实现, 它会尽可能长时间的存活于 JVM 内, 当没有任何对象指向它时 GC 执行后将会被回收 @Te ... 
- apropos linux
			Apropos adj. 恰当的,关于,就...而言 adv. 顺便地,恰当地 All my suggestions apropos the script were accepted. 我所有有关该剧 ... 
- 常用的ement语法
			缩写语法: 介绍:Emmet 使用类似于 CSS 选择器的语法描述元素在生成的文档树中的位置及其属性. 声明:第一次写博客大家多多关照,如有错误或者需要补充的请到评论里留言,谢谢大家! 快速生成htm ... 
- 使用notepad++远程编辑Linux文档
			上一篇中,我写了如何使用使用ftp服务器实现很方便的通信,这一篇我分享一个使用notepad++的一个NPPFTP插件远程编辑Linux中的文档的小技巧. 首先要确保你的Linux的ftp服务已经打开 ... 
- StatementHandler-Mybatis源码系列
			内容更新github地址:我飞 StatementHandler接口 StatementHandler封装了Mybatis连接数据库操作最基础的部分.因为,无论怎么封装,最终我们都是要使用JDBC和数 ... 
- Title Case a Sentence-freecodecamp算法题目
			Title Case a Sentence(中单词首字母大写) 要求 确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. 思路 将句子小写化后用.split(& ... 
- MFC 菜单编程 -- 总结
			菜单结构 一个菜单栏可以有若干个子菜单,而一个子菜单又可有若干个菜单项.对于菜单栏的子菜单,由左至右从0开始索引.对于特定的子菜单的菜单项,由上至下建立从0开始的索引.访问子菜单和菜单项,均可通过其索 ... 
- NOIP模拟赛 czy的后宫4
			czy的后宫4 [问题描述] czy有很多妹子,妹子虽然数量很多,但是质量不容乐观,她们的美丽值全部为负数(喜闻乐见). czy每天都要带N个妹子到机房,她们都有一个独一无二的美丽值,美丽值为-1到- ... 
- ERROR 1045 (28000): Access denied for user 'xxx'@'localhost' (using password: YES)  MYSQL 新建用户 无法登录 问题解决方法
			使用mysql ,出现新建账户无法登录问题 查看 user列表中,有部分账户没有设置密码,将全部重新设置一遍密码,然后还是无法登录. 使用命令 update user set password=pas ... 
