动态规划,转移方程为 dp[i][j] = (dp[i-1][j]+dp[i][j-1])%MOD,定义还是比较裸的,讨论一下就可以了
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define MOD 1000000007 int dp[3010],v[3010]; char str[6010]; int main() { int t,i,j; int n; scanf("%d",&t); while(t--) { scanf("%d",&n); scanf("%s",str+1); dp[0] = 1; v[0] = 1; for(i=0; i<=n; i++) for (j=0; j<=n; j++) { if(i==0 && j==0) continue; int sum = 0; if(str[i+j] == 'W') { if(i&1) sum = (sum+v[j]) % MOD; if(j&1) sum = (sum+dp[j-1]) % MOD; v[j] = dp[j] = sum % MOD; } else { if(i>0 && (i&1)==0) sum = (sum+v[j]) % MOD; if(j>0 && (j&1)==0) sum = (sum + dp[j-1]) % MOD; v[j] = dp[j] = sum % MOD; } } printf("%d ",dp[n]); } return 0; }