http://www.lydsy.com/JudgeOnline/problem.php?id=2958
http://www.lydsy.com/JudgeOnline/problem.php?id=3269
很麻烦的DP
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
long long f[1100000][3][2];//f[i][j][k] j:状态 0=没有连续k个的B/W 1=只有B有 2=BW都有 k:这个位置放 B0 W1
int w[1100000],b[1100000];
char s[1100000];
int m=1e9+7;
int main()
{
int n,k;
scanf("%d%d",&n,&k);
scanf("%s",s+1);
for (int i=1;i<=n;i++)
{
b[i]=b[i-1];w[i]=w[i-1];
if (s[i]=='B') b[i]++;
else if (s[i]=='W') w[i]++;
}
memset(f,0,sizeof(f));
f[0][0][1]=1;//B在左边所以0的位置先放个W
for (int i=1;i<=n;i++)
{
if (s[i]=='B'||s[i]=='X')
{
for (int j=0;j<=2;j++)
f[i][j][0]=(f[i-1][j][0]+f[i-1][j][1]+m)%m;
}
if (s[i]=='W'||s[i]=='X')
{
for (int j=0;j<=2;j++)
f[i][j][1]=(f[i-1][j][0]+f[i-1][j][1]+m)%m;
}
if (i<k) continue;
if ((s[i]=='B'||s[i]=='X')&&w[i]==w[i-k]) //i-k+1~i能够成为一个长度k的B串 (第i-k位一定为W否则长度为k+1)
{
f[i][1][0]=(f[i][1][0]+f[i-k][0][1]+m)%m;
f[i][0][0]=(f[i][0][0]-f[i-k][0][1]+m)%m;
}
if ((s[i]=='W'||s[i]=='X')&&b[i]==b[i-k]) //i-k+1~i能够成为一个 长度k的W串(第i-k位一定位B否则长度为k+1)
{
f[i][2][1]=(f[i][2][1]+f[i-k][1][0]+m)%m;
f[i][1][1]=(f[i][1][1]-f[i-k][1][0]+m)%m;
}
}
printf("%lld\n",(f[n][2][0]+f[n][2][1]+m)%m);
}