题意:
解法:
这题有点搞心态,没想到正解这么简单,有点偏思维了...
---
假设串的长度为len,
由于是二进制串,不同串的总数为2^len.
而题目给定串只是其中的一种.
我们令d[i][j]为i次操作,弄出长度为j的串的方案数,
d[n][len]可以拼出2^len种串,且概率相同,
那么拼出给定串的方案数为d[n][len]/(2^len).
code:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxm=5e3+5;
const int mod=1e9+7;
int d[maxm][maxm];
char s[maxm];
int n;
/*
d[i][j]表示i次,现在有j个字符的方案数.
*/
int ppow(int a,int b,int mod){
int ans=1%mod;a%=mod;
while(b){
if(b&1)ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
void solve(){
cin>>n>>(s+1);
d[0][0]=1;
for(int i=0;i<n;i++){
for(int j=0;j<=n;j++){
if(!d[i][j])continue;
//删除
if(!j)d[i+1][j]=(d[i+1][j]+d[i][j])%mod;
else d[i+1][j-1]=(d[i+1][j-1]+d[i][j])%mod;
//添加
if(j+1<=n)d[i+1][j+1]=(d[i+1][j+1]+d[i][j]*2)%mod;
}
}
int len=strlen(s+1);
int ans=d[n][len];
int inv=ppow(ppow(2,len,mod),mod-2,mod);
ans=ans*inv%mod;
cout<<ans<<endl;
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);
solve();
return 0;
}