hdu 5396 Expression 2015多校联合训练赛#9 动态规划 dp

Expression

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 372    Accepted Submission(s): 209


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
 



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define maxn 102
int mod = 1000000007;
ll dp[maxn][maxn];
int num[maxn];
char word[maxn];
ll C[maxn][maxn];
ll A[maxn];
int main(){
    int n;
    memset(C,0,sizeof(C));
    A[0] = 1;
    for(int i = 1;i < maxn; i++)
        A[i] = A[i-1]*i%mod;
    for(int i = 0;i < maxn; i++){
        C[i][0] = 1;
        for(int j = 1;j <= i; j++)
            C[i][j] = (C[i-1][j]+C[i-1][j-1])%mod;
    }
    ll res = 0;
    while(scanf("%d",&n)!=EOF){
        for(int i = 0;i < n; i++)
            scanf("%d",&num[i]);
        scanf("%s",word);
        memset(dp,0,sizeof(dp));
        for(int i = 0;i < n; i++)
            dp[i][i] = num[i];
        for(int i = 1;i < n; i++){
            for(int j = 0;j + i < n; j++){
                for(int k = j;k < j+i; k++){
                    if(word[k] == '*')
                        res = dp[j][k]*dp[k+1][j+i]%mod;
                    else if(word[k] == '+')
                        res = (dp[j][k]*A[j+i-k-1]%mod+dp[k+1][j+i]*A[k-j]%mod);
                    else
                        res = (dp[j][k]*A[j+i-k-1]%mod-dp[k+1][j+i]*A[k-j]%mod);

                    dp[j][j+i] = (dp[j][j+i]+res*C[i-1][k-j])%mod;
                }
            }
        }
        dp[0][n-1] = (dp[0][n-1]%mod+mod)%mod;
        cout<<dp[0][n-1]<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值