参考:点击打开链接
解析:dp[i][j]为前i个字符 有j个U没有确定位置的方案数。
则
dp[i][j] = dp[i-1][j]*j+dp[i-1][j+1]*(j+1)*(j+1) (s[i] = 'D')
dp[i][j] = dp[i-1][j-1]+dp[i-1][j]*j (s[i] = 'U')
[code]:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define th(x) this->x=x;
using namespace std;
typedef long long LL;
const int maxn = 1005;
const LL MOD = 1e9+7;
char s[maxn];
int n;
LL dp[maxn][maxn];
void sol(){
int i,j;
dp[0][0] = 1;
for(i = 1;i <= n;i++){
for(j = 0;j <= n;j++){
dp[i][j] = 0;
if(s[i]=='D'){
dp[i][j] = (dp[i][j]+dp[i-1][j]*j)%MOD;
if(j+1<=n) dp[i][j] = (dp[i][j]+dp[i-1][j+1]*(j+1)*(j+1))%MOD;
}else{
dp[i][j] = (dp[i][j] + dp[i-1][j-1])%MOD;
dp[i][j] = (dp[i][j] + dp[i-1][j]*j)%MOD;
}
}
}
printf("%lld\n",(dp[n][0]%MOD+MOD)%MOD);
}
int main(){
int i,j,cas;
scanf("%d",&cas);
for(int T=1;T<=cas;T++){
scanf("%s",s+1);
n = strlen(s+1);
for(i = 1;i <= n;i++){
if(s[i]=='E'){
for(j = i;j < n;j++) swap(s[j],s[j+1]);
n--,i--;
}
}
printf("Case %d: ",T);
sol();
}
return 0;
}