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重定向终于过了。枚举所有括号的位置,然后按照括号和加法乘法优先级进行模拟运算再取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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值