HDU 4055 Number String 计数DP
定义一个只含 的数字串 的签名如下,假如当前大于前一位,则当前位记为 ,否则记为 ,比如:
the signature of the permutation {3,1,2,7,4,6,5} is “DIIDID”.
现在给一个长度为 的签名,问符合该签名的数字串有多少个?
用 表示前 个数,末尾是 对应的个数,按情况讨论(令 ):
(1):
(2):
(3):
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 1010
#define MOD 1000000007
using namespace std;
typedef long long ll;
char a[MAXN];
ll dp[MAXN][MAXN],s[MAXN][MAXN];
int main(){
#ifdef WINE
freopen("data.in","r",stdin);
#endif
while(scanf(" %s ",a)!=EOF){
int n=strlen(a)+1;
dp[1][1]=s[1][1]=1;
for(int i=2;i<=n;i++){
char c=a[i-2];
for(int j=1;j<=n;j++){
if(c=='?')
dp[i][j]=s[i-1][i-1];
else if(c=='I')
dp[i][j]=s[i-1][j-1];
else dp[i][j]=(s[i-1][i-1]-s[i-1][j-1]+MOD)%MOD;
s[i][j]=(s[i][j-1]+dp[i][j])%MOD;
}
}
printf("%lld\n",s[n][n]);
}
return 0;
}