Little Ruins is a studious boy, recently he learned the four operations!
Now he want to use four operations to generate a number, he takes a string which only contains digits '1' - '9', and split it into 5
intervals and add the four operations '+', '-', '*' and '/' in order, then calculate the result(/ used as integer division).
Now please help him to get the largest result.
Input
First line contains an integer T
, which indicates the number of test cases.
Every test contains one line with a string only contains digits '1'- '9'.
Limits
1≤T≤105
5≤length of string≤20
Output
For every test case, you should output 'Case #x: y', where x indicates the case number and counts from 1 and y is the result.
Sample Input
1 12345
Sample Output
Case #1: 1
题意:给你一个只有“1~9”数字的字符串,求按顺序插入“+”,“-”,“*”,“/”后,算式能够得到的最大值。
(思路:因为有“-”,那么我们就要使“-”后边的数尽可能的小,又因为“-”后面是“*”,想要保证数小的话,那么就让“*”两边的数尽可能小,“/”后面的数尽可能大。我们枚举“-”的位置,对于“-”前面的数,我们枚举“+”的位置,取最大值;然后使“*”两边的数都只有一位,剩下的数全都放在“/”后面,就好了。)
(PS:这个题写了半天搜索,还用到了算术表达式的中缀转后缀,后缀计算结果。结果超时。。。)
1.AC代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#define ll long long
using namespace std;
char s[30],ss[30];
int n;
ll ans;
int main(void)
{
int t;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++)
{
ans=-1e18;
scanf("%s",s+1);
n=strlen(s+1);
for(int i=2;i+3<=n;i++)
{
ll tmp=0,a=0,b=0;
for(int j=1;j<i;j++)
{
a=b=0;
for(int k=1;k<=j;k++)
{
a=a*10+s[k]-'0';
}
for(int k=j+1;k<=i;k++)
{
b=b*10+s[k]-'0';
}
// cout<<a<<"+"<<b;
tmp=max(tmp,a+b);
}
ll c=0;
a=s[i+1]-'0';
b=s[i+2]-'0';
for(int j=i+3;j<=n;j++)
{
c=c*10+s[j]-'0';
}
//cout<<"-"<<a<<"*"<<b<<"/"<<c<<endl;
ans=max(ans,tmp-a*b/c);
}
printf("Case #%d: %lld\n",cas,ans);
}
}
2.搜索TLE代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#define ll long long
using namespace std;
char s[30],ss[30];
struct node
{
int type;
char op;
ll num;
}sss[30];
int pri[10];
//0:+ 1:- 2:* 3:/
int n;
ll ans;
bool is_digit(char c)
{
return c>='1'&&c<='9';
}
ll solve(ll a,char c,ll b)
{
if(c=='+') return a+b;
else if(c=='-') return a-b;
else if(c=='*') return a*b;
else return a/b;
}
ll cal()
{
ll num;
node t;
char sta[10];
int top=0;
int cnt=0,len=strlen(ss);
for(int i=0;i<len;i++)
{
if(ss[i]>='1'&&ss[i]<='9')
{
int j;
num=0;
for(j=i;j<len&&is_digit(ss[j]);j++)
{
num=num*10+ss[j]-'0';
}
t.type=1;
t.num=num;
sss[cnt++]=t;
i=j-1;
}
else
{
while(top&&pri[ss[i]]<=pri[sta[top]])
{
t.type=0;
t.op=sta[top];
//sta.pop();
top--;
sss[cnt++]=t;
}
//sta.push(ss[i]);
sta[++top]=ss[i];
}
}
//cout<<sta.size()<<endl;
while(top)
{
t.type=0;
t.op=sta[top];
//sta.pop();
top--;
sss[cnt++]=t;
}
//stack <ll> st;
ll st[50];
int top1=0;
for(int i=0;i<cnt;i++)
{
if(sss[i].type)
{
//cout<<sss[i].num<<endl;
//st.push(sss[i].num);
st[++top1]=sss[i].num;
}
else
{
//cout<<sss[i].op<<endl;
//ll b=st.top();
ll b=st[top1];
top1--;
//st.pop();
ll a=st[top1];//st.top();
//st.pop();
top1--;
//st.push(solve(a,sss[i].op,b));
st[++top1]=solve(a,sss[i].op,b);
}
}
return st[top1];
}
char get(int op)
{
if(op==0) return '+';
else if(op==1) return '-';
else if(op==2) return '*';
else return '/';
}
void dfs(int pos,int in,int op)
{
if(op==4)
{
if(pos<n)
{
while(pos<n)
{
ss[in++]=s[pos++];
}
ss[in]='\0';
// cout<<ss<<"\n";
// cout<<cal()<<endl;
ans=max(ans,cal());
}
return ;
}
else
{
for(int i=pos;i+4-op<n;i++)
{
ss[in++]=s[i];
ss[in++]=get(op);
dfs(i+1,in,op+1);
in--;
}
return ;
}
}
int main(void)
{
pri['*']=pri['/']=2;
pri['+']=pri['-']=1;
int t;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++)
{
ans=0;
scanf("%s",s);
n=strlen(s);
dfs(0,0,0);
printf("Case #%d: %lld\n",cas,ans);
}
}
/*
12345678912345678999
*/