hdu 5396 구간 dp + 조합

http://acm.hdu.edu.cn/showproblem.php?pid=5396
Problem Description
Teacher Mai has 
n  numbers 
a1,a2,⋯,an and 
n−1  operators("+", "-" or "*")
op1,op2,⋯,opn−1 , 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+1−i  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 
n−1  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+4∗6−8∗3 " is 
1+4∗6−8∗3→1+4∗(−2)∗3→1+(−8)∗3→(−7)∗3→−21 .
 
Input
There are multiple test cases.
For each test case, the first line contains one number 
n(2≤n≤100) .
The second line contains 
n  integers 
a1,a2,⋯,an(0≤ai≤109) .
The third line contains a string with length 
n−1  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.
/**
hdu 5396    dp+  
    :       +-*    ,           ,               ?
    :        dp            ,   ,                  ==
           ( )  dp[l][r]   l    r                 ,t[l][r]   l    r
                      。dp[l][r]                  i, n1=dp[l][i],
           n2=dp[i+1][r],t1=t[l][i],t2=t[i+1][r]。      ,    i   n1*t2+n2*t1,      
              ,            ,         ,            。  n1   t2 ,
           n2   t1 。       。     n1*n2。    ,                   ,
                f1   ,   f2   ,  C[f1+f2][f1]   ,    f1+f2     f1 ,    f2,
           f1 f2           ,      C[f1+f2][f1]。      i,t[l][r]   t1*t2*C[f1+f2][f1]。
*/
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
int n;
LL a[105],dp[105][105],num[105][105],c[105][105];
char s[105];

char getC()
{
    c[0][0]=1;
    for(int i=1;i<=100;i++)
    {
        c[i][0]=c[i][i]=1;
        for(int j=1;j<i;j++)
        {
            c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
        }
    }
}
int main()
{
    getC();
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++)
            scanf("%I64d",&a[i]);
        scanf("%s",s+1);
        memset(num,0,sizeof(num));
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=n; i++)
        {
            num[i][i]=1;
            dp[i][i]=a[i];
        }
        for(int i=n-1; i>=1; i--)
        {
            for(int j=i+1; j<=n; j++)
            {
                LL cnt=0;
                for(int k=i; k<j; k++)
                {
                    if(s[k]=='*')
                    {
                        cnt=(dp[i][k]*dp[k+1][j])%mod;
                    }
                    else if(s[k]=='-')
                    {
                        cnt=((dp[i][k]*num[k+1][j])%mod-(dp[k+1][j]*num[i][k])%mod)%mod;
                    }
                    else
                    {
                        cnt=((dp[i][k]*num[k+1][j])%mod+(dp[k+1][j]*num[i][k])%mod)%mod;
                    }
                    dp[i][j]=(dp[i][j]+c[j-i-1][k-i]*cnt%mod)%mod;
                    num[i][j]=(num[i][j]+num[i][k]*num[k+1][j]%mod*c[j-i-1][k-i]%mod)%mod;
                }
                //printf("%d %d:%I64d
",i,j,dp[i][j]); } } printf("%I64d
",(dp[1][n]+mod)%mod); } return 0; }

좋은 웹페이지 즐겨찾기