hdu5396Expression(区间dp,好题)

本文介绍了一种针对给定数字序列及运算符序列求所有可能运算组合结果之和的算法。通过动态规划的方法,实现了一个可以高效计算任意给定序列的解决方案,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                      Expression

                                                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others

Problem Description
Teacher Mai has n numbers a1,a2,,an and n1 operators("+", "-" or "*") op1,op2,,opn1 , which are arranged in the form a1 op1 a2 op2 a3  an .

He wants to erase numbers one by one. In i -th round, there are n+1i numbers remained. He can erase two adjacent numbers and the operator between them, and then put a new number (derived from this one operation) in this position. After n1 rounds, there is the only one number remained. The result of this sequence of operations is the last number remained.


He wants to know the sum of results of all different sequences of operations. Two sequences of operations are considered different if and only if in one round he chooses different numbers.

For example, a possible sequence of operations for " 1+4683 " is 1+46831+4(2)31+(8)3(7)321 .
 

Input
There are multiple test cases.

For each test case, the first line contains one number n(2n100) .

The second line contains n integers a1,a2,,an(0ai109) .

The third line contains a string with length n1 consisting "+","-" and "*", which represents the operator sequence.
 

Output
For each test case print the answer modulo 109+7 .
 

Sample Input
  
  
3 3 2 1 -+ 5 1 4 6 8 3 +*-*
 

Sample Output
  
  
2 999999689
Hint
Two numbers are considered different when they are in different positions.
 

Author
xudyh
 

Source
2015 Multi-University Training Contest 9

首先,如果将要进行的符号运算是*,设左边运算出的答案分别为a1,a2,a3...an,右边的答案为b1,b2,b3,b4...bn;
则左边乘以右边为a1*(右边的和)+a2*(右边的和)+...,也为dp[l][k]*dp[k+1][r];
如果将要进行的符号运算是+,设左边有n=(k-l)!组答案,右边为有m=(r-k)组答案,
设左边每组答案分别为a1,a2,a3...,右边每组答案为b1,b2,b3....
所以答案为a1+b1+a1+b2+...a1+bn=(右边的情况数*ai-右边的和)*左边的情况数= d pl,r ( r k 1 ) ! + d pk+1,r ( k l ) !
最后要确定左边操作和右边操作的顺序 因为每个答案里是统计了该区间所有的阶乘情况,因此每一个左边已确定的顺序和右边已确定的顺序需要排列组合一下。比如:左边有3个操作数+-*,右边有2个操作符+-,当已经确定了他们各自的顺序,假设左边算-*+,右边+-,这个顺序已经固定,现在有五个操作符需要操作,我需要选择三个位置给左边的操作符-*+,那么右边的两个操作符自然就对应他们相应的位置。
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
char s[111];
ll dp[111][111];
int num[111];
ll A[111];
ll C[111][111];

void solve(int n){
    for(int k=2;k<=n;k++)   //长度
        for(int i=1;i+k-1<=n;i++)    //从哪里开始
            for(int j=i;j-i+1<k;j++){      //到哪里结束
                if(s[j]=='-'){
                    dp[i][i+k-1]=(dp[i][i+k-1]+(A[i+k-j-2]*dp[i][j]%MOD-A[j-i]*dp[j+1][i+k-1]%MOD)*C[k-2][j-i]%MOD)%MOD;
                    //cout<<"i is "<<i<<" j is "<<j<<" k is "<<k<<" dp["<<i<<"]["<<i+k-1<<"] is "<<dp[i][i+k-1]<<endl;
                }
                else if(s[j]=='+'){
                    dp[i][i+k-1]=(dp[i][i+k-1]+(A[j-i]*dp[j+1][i+k-1]%MOD+A[i+k-j-2]*dp[i][j]%MOD)*C[k-2][j-i]%MOD)%MOD;
                    //cout<<"i is "<<i<<" j is "<<j<<" k is "<<k<<" dp["<<i<<"]["<<i+k-1<<"] is "<<dp[i][i+k-1]<<endl;
                }
                else if(s[j]=='*'){
                    dp[i][i+k-1]=(dp[i][i+k-1]+dp[j+1][i+k-1]*dp[i][j]%MOD*C[k-2][j-i]%MOD)%MOD;
                    //cout<<"i is "<<i<<" j is "<<j<<" k is "<<k<<" dp["<<i<<"]["<<i+k-1<<"] is "<<dp[i][i+k-1]<<endl;
                }
            }
}

void init(){
    A[0]=1;
    A[1]=1;
    for(int i=2;i<=101;i++)
        A[i]=A[i-1]*i%MOD;
    for(int i=0;i<101;i++){
        C[i][0]=C[i][i]=1;
        for(int j=1;j<=i;j++)
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%MOD;
    }
}

int main(){
    int n;
    init();
    while(scanf("%d",&n)!=EOF){
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++){
            scanf("%d",&num[i]);
            dp[i][i]=num[i];
        }
        scanf("%s",s+1);
        solve(n);
        printf("%I64d\n",(dp[1][n]%MOD+MOD)%MOD);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值