hdu 4055 动态规划

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Maxn 1010
#define Maxm 100010
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 0x7fffffff
#define Mod 1000000007
using namespace std;
int dp[Maxn][Maxn],sum[Maxn][Maxn];
char str[Maxn];
int main()
{
    int i,j,n;
    while(scanf("%s",str+2)!=EOF){
        memset(dp,0,sizeof(dp));
        dp[1][1]=1;
        sum[1][1]=1;
        n=strlen(str+2);
        for(i=2;i<=n+1;i++){
            if(str[i]=='I'||str[i]=='?'){
                for(j=1;j<=i;j++){
                    dp[i][j]+=(sum[i-1][j-1])%Mod;
                    dp[i][j]%=Mod;
                    sum[i][j]=sum[i][j-1]%Mod+dp[i][j]%Mod;
                    sum[i][j]%=Mod;
                }
            }
            if(str[i]=='D'||str[i]=='?')
            {
                for(j=1;j<=i;j++){
                    dp[i][j]+=(sum[i-1][i-1]%Mod-sum[i-1][j-1]%Mod+Mod)%Mod;
                    dp[i][j]%=Mod;
                    sum[i][j]=sum[i][j-1]%Mod+dp[i][j]%Mod;
                    sum[i][j]%=Mod;
                }
            }
        }
        int ans=0;
        for(i=1;i<=n+1;i++){
            ans+=dp[n+1][i];
            ans%=Mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2013-08-30 20:38  fangguo  阅读(192)  评论(0编辑  收藏  举报