2019年ICPC南昌网络赛 D. Match Stick Game (记搜)

题库链接:https://nanti.jisuanke.com/t/38223

Xiao Ming recently indulges in match stick game and he thinks he is good at it. His friend Xiao Jun decides to test him. Xiao Jun gives him an expression of length , made by match sticks and asks him to calculate the maximum value of the expression by moving any match sticks (but he can’t discard any of them). The expression is made up of some numbers, plus signs and minus signs represented as A_1 \ op_1 \ A_2 \ op_2 \ A_3 \ op_3 \ \cdots A_{m - 1} \ op_{m - 1} \ A_mA1​ op1​ A2​ op2​ A3​ op3​ ⋯Am−1​ opm−1​ Am​. mm must be count by himself, A_k(1 \le k \le m)Ak​(1≤k≤m) is an integer without leading zeros and less than 10^9109 , op_k (1 \le k \le m)opk​(1≤k≤m) is a plus sign or a minus sign. At the same time, there are some requirements of the new expression:

  1. The new expression should also be made up of mm numbers and m - 1m−1 operators.
  2. The number of digits per number should keep consistent with the original.
  3. There couldn’t be any leading zeros per number.

2019年ICPC南昌网络赛 D. Match Stick Game (记搜)_第1张图片

Input

The first line consists of a single integer TT denoting the number of test cases.

There’re two lines in each test case.

The first line contains an integer nn.

A string of length nn follows in the next line, denoting the expression given.

The expression is guaranteed to be valid.

Output

For each test case, print a single integer denoting the maximum result of the expression.

Constraints

1 \le n \le 1001≤n≤100

Note

Expression with the maximum result for the second sample is 7 - 17−1 .

Expression with the maximum result for the second sample is 7 + 7 + 97+7+9.

样例输入复制

3
3
1-1
3
1+1
5
1+2+3

样例输出复制

0
6
23

题意:

给你一个由n(n<=100)个火柴棒摆成的m个数和m-1个加减号的字符串表达式,问你任意移动火柴棒,使得数字位还是数字位,符号位还是符号位,长度不变且数字位不能有前导零的表达式的最大值。

例如 1+1 可以变成的表达式 7-1 值最大,答案是6

思路:

因为长度不超过100,总火柴棒不超过100*7(数字8所用的火柴数最大为7),那么我们就可以做一个到第i个字符,已经用了(或者还剩下)j根火柴棒所能表示的表达式最大值的记忆化搜索。

因为有减号,所以还要记录最小值(毕竟减最小值可以使结果最大)。

其实并不难,相比于树剖那道题还要简单些。。。(虽然树剖那道题也不算难)但是可惜没比赛时时间写了。。。

代码:

#include
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3fLL
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
#define mp(x,y)  make_pair(x,y)
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int maxn = 105;
int n,m,k,l;
pair dp[maxn][maxn*7];
char s[maxn];
int a[maxn*maxn],num,ok[maxn][maxn*7];
ll c[maxn];
int dxy[]={6,2,5,5,4,5,6,3,7,6};
pair dfs(int i,int j)
{
    pairtmp;
    tmp.first=-inf;
    tmp.second=inf;
    if(i==l)
    {
        if(j==num)
        {
            tmp.first=tmp.second=0;
            return tmp;
        }
        return tmp;
    }
    if(ok[i][j]) return dp[i][j];
    if(s[i]<'0'||s[i]>'9')
    {
        if(j+1<=num){
        pair aa=dfs(i+1,j+1);
        if(aa.first!=-inf) tmp.second=min(tmp.second,-aa.first);
        if(aa.second!=inf) tmp.first=max(tmp.first,-aa.second);
        }
        if(j+2<=num){
        pair aa=dfs(i+1,j+2);
        if(aa.second!=inf) tmp.second=min(tmp.second,aa.second);
        if(aa.first!=-inf) tmp.first=max(tmp.first,aa.first);
        }
    }
    else
    {
        pair x;
        rep(k,0,9)
        if(j+dxy[k]<=num)
        {
            if((!i||s[i-1]=='-'||s[i-1]=='+')&&!k) continue;
            x=dfs(i+1,j+dxy[k]);
            if(x.first!=-inf) tmp.first=max(tmp.first,k*c[i]+x.first);
            if(x.second!=inf) tmp.second=max(tmp.first,k*c[i]+x.second);
        }
    }
    ok[i][j]=1;
    dp[i][j]=tmp;
    return tmp;
}
int main()
{
    a['+']=2;
    a['-']=1;
    a['0']=6;
    a['1']=2;
    a['2']=5;
    a['3']=5;
    a['4']=4;
    a['5']=5;
    a['6']=6;
    a['7']=3;
    a['8']=7;
    a['9']=6;
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        num=0;
        scanf("%s",s);
        //cout<<(int)(strlen(s))<<" * "<ans;
        ans=dfs(0,0);
        printf("%lld\n",ans.first);
    }
    return 0;
}
/*
4
3
1-1
3
1+1
5
1+2+3
99
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
*/

 

你可能感兴趣的:(动态规划,经典dp)