Original question: fzu 2170 http://acm.fzu.edu.cn/problem.php? PID = 1, 2170
I did not understand the question at the time, and I didn't even think about the example. So I still feel that it is not easy to do this. Some questions are obviously very simple, but I gave up and even went to play because I didn't understand it. This would not achieve much effect.
Solution:
Definition: DP [I] [J]: The number of labeling schemes in which J of the first I letter belongs to the first sequence.
In case of 'B', the first sequence must be an odd number, that is, J & 1 is transferred. When the number of the second sequence is an odd number (I-j) & 1), it also needs to be transferred because B may belong to the second sequence. Otherwise.
Use a scrolling array to save space.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#define Mod 1000000007using namespace std;#define N 6007int dp[2][N];char ss[N];int main(){ int n,i,j; int now; int t; scanf("%d",&t); while(t--) { scanf("%d",&n); scanf("%s",ss); memset(dp,0,sizeof(dp)); dp[0][0] = 1; for(i=0,now=1;i<2*n;i++,now=1-now) { memset(dp[now],0,sizeof(dp[now])); if(ss[i] == ‘B‘) { for(j=0;j<=n;j++) { if(j&1) dp[now][j+1] = (dp[now][j+1]+dp[i&1][j])%Mod; if((i-j)&1) dp[now][j] = (dp[now][j]+dp[i&1][j])%Mod; } } else { for(j=0;j<=n;j++) { if((j&1) == 0) dp[now][j+1] = (dp[now][j+1]+dp[i&1][j])%Mod; if(((i-j)&1) == 0) dp[now][j] = (dp[now][j]+dp[i&1][j])%Mod; } } } printf("%d\n",dp[0][n]); } return 0;}View code