hdu 4055 dp
不怎么会做这类题目额。。伤,看别人的
View Code
1 #include<stdio.h>
2 #include<string.h>
3 #define maxn 1010
4 int dp[maxn][maxn];
5 int sum[maxn][maxn];
6 const int mod = 1000000007;
7 int main()
8 {
9 int i,j;
10 char s[maxn];
11 while(scanf("%s",s)!=EOF)
12 {
13 int n=strlen(s);
14 memset(dp,0,sizeof(dp));
15 memset(sum,0,sizeof(sum));
16 for(i=1;i<=n+1;i++) sum[0][i]=1;
17 for(i=1;i<=n;i++)
18 {
19 for(j=1;j<=i+1;j++)
20 {
21 if(s[i-1]=='I'||s[i-1]=='?')
22 {
23 dp[i][j]+=sum[i-1][j-1];
24 }
25 if(s[i-1]=='D'||s[i-1]=='?')
26 {
27 dp[i][j]+=(sum[i-1][i]-sum[i-1][j-1]);
28 dp[i][j]=(dp[i][j]+mod)%mod;
29 }
30 sum[i][j]+=(sum[i][j-1]+dp[i][j])%mod;
31 }
32 }
33 int ans=0;
34 for(i=0;i<=n+1;i++)
35 ans=(ans+dp[n][i])%mod;
36 printf("%d\n",ans);
37 }
38 return 0;
39 }