【DP】【七中联考】【B】

``                     **B题**``

?D

3

动态规划裸题

F[i][j]=∑F[i-1][k] (1<=k<=j-1)

F[i][j]=∑F[i-1][k] (j<=k<=i-1)

（都一样的。）

``#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#define LL long long using namespace std;LL dp[3005][3005],i=1,ans;char s;int main(){    const LL mod=1e9+7;    freopen("B.in","r",stdin);        //freopen("B.out","w",stdout);    dp[1][1]=1;    s=getchar();    while(s == 'I'||s == 'D'||s == '?')     {        for (LL j=1;j<=i;j++)            dp[i][j]=(dp[i][j]+dp[i][j-1]+mod)%mod;         //前缀和优化         i++;        if(s=='I')            for (LL j=2;j<=i;j++)                    dp[i][j] =(dp[i][j]+ dp[i-1][j-1]-dp[i-1][0]+mod)%mod;        if(s=='D')            for (LL j=1;j<=i;j++)                    dp[i][j] =(dp[i][j]+ dp[i-1][i-1]-dp[i-1][j-1]+mod)%mod;        if(s=='?'){            for (LL j=2;j<=i;j++)                dp[i][j] =(dp[i][j]+ dp[i-1][j-1]-dp[i-1][0]+mod)%mod;            for (LL j=1;j<=i;j++)                dp[i][j] =(dp[i][j]+ dp[i-1][i-1]-dp[i-1][j-1]+mod)%mod;        }        s=getchar();    }    for(LL j = 1;j <= i;j++)        ans = (ans + dp[i][j]+mod) % mod;    printf("%I64d",ans);    return 0;}``
0 0