hdu5396Expression(区间dp,好题)

                                                      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;
}


你可能感兴趣的:(hdu5396Expression(区间dp,好题))