HDU-5938:Four Operations(DP)

来源:互联网 发布:化工项目网络计划绘制 编辑:程序博客网 时间:2024/05/17 09:15

Four Operations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2082    Accepted Submission(s): 608


Problem Description
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
1T105
5length of string20
 

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
112345
 

Sample Output
Case #1: 1

思路:d[i]表示[0,i]间放'+'的最大值,f[i]表示[i,n]间放'*'和'/'后的最小值。ans=max(d[i]-f[i+1]);

#include<bits/stdc++.h>using namespace std;char s[30];long long d[30],f[30],n;long long num(int x,int y){    long long sum=0;    for(int i=x;i<=y;i++)sum=sum*10+s[i]-'0';    return sum;}int main(){    int T,cas=1;cin>>T;    while(T--)    {        memset(d,0,sizeof d);        memset(f,1e9+7,sizeof f);        scanf("%s",s);        n=strlen(s);        for(int i=0;i<n-3;i++)        {            for(int j=0;j<i;j++)d[i]=max(d[i],num(0,j)+num(j+1,i));        }        for(int i=n-1;i>=2;i--)        {            for(int j=i;j<=n-1;j++)            {                for(int k=j+1;k<n-1;k++)f[i]=min(f[i],num(i,j)*num(j+1,k)/num(k+1,n-1));            }        }        long long ans=-1e9-7;        for(int i=1;i<n-3;i++)ans=max(ans,d[i]-f[i+1]);        printf("Case #%d: %lld\n",cas++,ans);    }    return 0;}


原创粉丝点击