最值问题
Time Limit: 1000MS Memory limit: 65536K
题目描述
最值问题一直是我们比较关注的问题,设想一下当给定一个不含括号算数表达式后,加入适当的括号,必会取得最值。为了简化问题今天我们只考虑算术运算符是‘+’和‘*’的情况
输入
第一行一个数字t,代表输入的组数
第二行起: 输入算数表达式,数据保证 表达式中最多有12个数字,并且数字在1到20之间。运算符为‘+’或‘*’,并且表达式中没有 空格
输出
对于每组数据,输出对应的最大值和最小值
示例输入
1 2*1+1
示例输出
4 3
提示
#include<iostream>
#include<cstdio>
#include<string.h>
#include<map>
#include<stack>
#define max 10001
using namespace std;
map<char,int> ma;
char root[max];
char cha1[max],cha2[max];
int Judge(char a)
{
if(a>='a'&&a<='z')
return 1;
return 0;
}
void Convert(char ch[])
{
stack<char> start;
int len=strlen(ch);
int i;
int Top=0;
for(i=0; i<len; i++)
{
if(Judge(ch[i]))
{
root[Top++]=ch[i];
}
else
{
switch(ch[i])
{
case '+':
case '*':
while((!start.empty())&&ma[start.top()]>=ma[ch[i]])
{
root[Top++]=start.top();
start.pop();
}
start.push(ch[i]);
break;
}
}
}
while(!start.empty())
{
root[Top++]=start.top();
start.pop();
}
root[Top]=0;
}
long long sum()
{
stack<long long> end;
int len=strlen(root);
int i;
for( i=0; i<len; i++)
{
if(Judge(root[i]))
{
end.push(root[i]-'a');
}
else
{
long long num1,num2;
num1=end.top();
end.pop();
num2=end.top();
end.pop();
switch(root[i])
{
case '*':
end.push(num1*num2);
break;
case '+':
end.push(num1+num2);
break;
}
}
}
return end.top();
}
int main()
{
int N;
long long ans;
cin>>N;
long long a,b;
while(N--)
{
a=b=0;
memset(cha1,0,sizeof(cha1));
memset(cha2,0,sizeof(cha2));
scanf("%s",cha1);
int len=strlen(cha1);
int step=-1;
ans=0;
for(int i=0; i<len; i++)
{
if(cha1[i]=='*'||cha1[i]=='+')
{
cha2[++step]=ans/10+'a';
cha2[++step]=cha1[i];
ans=0;
continue;
}
ans=(ans+(cha1[i]-'0'))*10;
}
cha2[++step]=ans/10+'a';
ma['+']=4;
ma['*']=2;
memset(root,0,sizeof(root));
Convert(cha2);
a=sum();
ma['+']=1;
ma['*']=2;
memset(root,0,sizeof(root));
Convert(cha2);
b=sum();
printf("%lld %lld\n",a,b);
}
return 0;
}
/*
12
20*20*20*20*20*20*20*20*20*20*20*20
1358954496 1358954496
1358954496
*/
#include<cstdio>
#include<string.h>
#include<map>
#include<stack>
#define max 10001
using namespace std;
map<char,int> ma;
char root[max];
char cha1[max],cha2[max];
int Judge(char a)
{
if(a>='a'&&a<='z')
return 1;
return 0;
}
void Convert(char ch[])
{
stack<char> start;
int len=strlen(ch);
int i;
int Top=0;
for(i=0; i<len; i++)
{
if(Judge(ch[i]))
{
root[Top++]=ch[i];
}
else
{
switch(ch[i])
{
case '+':
case '*':
while((!start.empty())&&ma[start.top()]>=ma[ch[i]])
{
root[Top++]=start.top();
start.pop();
}
start.push(ch[i]);
break;
}
}
}
while(!start.empty())
{
root[Top++]=start.top();
start.pop();
}
root[Top]=0;
}
long long sum()
{
stack<long long> end;
int len=strlen(root);
int i;
for( i=0; i<len; i++)
{
if(Judge(root[i]))
{
end.push(root[i]-'a');
}
else
{
long long num1,num2;
num1=end.top();
end.pop();
num2=end.top();
end.pop();
switch(root[i])
{
case '*':
end.push(num1*num2);
break;
case '+':
end.push(num1+num2);
break;
}
}
}
return end.top();
}
int main()
{
int N;
long long ans;
cin>>N;
long long a,b;
while(N--)
{
a=b=0;
memset(cha1,0,sizeof(cha1));
memset(cha2,0,sizeof(cha2));
scanf("%s",cha1);
int len=strlen(cha1);
int step=-1;
ans=0;
for(int i=0; i<len; i++)
{
if(cha1[i]=='*'||cha1[i]=='+')
{
cha2[++step]=ans/10+'a';
cha2[++step]=cha1[i];
ans=0;
continue;
}
ans=(ans+(cha1[i]-'0'))*10;
}
cha2[++step]=ans/10+'a';
ma['+']=4;
ma['*']=2;
memset(root,0,sizeof(root));
Convert(cha2);
a=sum();
ma['+']=1;
ma['*']=2;
memset(root,0,sizeof(root));
Convert(cha2);
b=sum();
printf("%lld %lld\n",a,b);
}
return 0;
}
/*
12
20*20*20*20*20*20*20*20*20*20*20*20
1358954496 1358954496
1358954496
*/