[BZOJ3864]

[BZOJ3864] Hero meet devil 状压DP(DP套DP)

[BZOJ3864] Hero meet devil 状压DP(DP套DP)

Code


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
using namespace std;
const int mod=1e9+7;
const int N=1001;
int f[N][(1<<15)+10],sta[(1<<15)+10][5],tmp[2][20],ans[20],T,n,m;
string s="ACGT";
char a[N];
inline void init(){
	memset(f,0,sizeof(f));
	memset(ans,0,sizeof(ans));
}
int work(int S,char ch){
	memset(tmp,0,sizeof(tmp));
	rep(i,1,n)tmp[0][i]=tmp[0][i-1]+((S>>(i-1))&1);
	rep(i,1,n){
		if(a[i]==ch)tmp[1][i]=tmp[0][i-1]+1;
		else tmp[1][i]=max(tmp[0][i],tmp[1][i-1]);
	}
	int tot=0;
	rep(i,1,n)tot+=(1<<(i-1))*(tmp[1][i]-tmp[1][i-1]);
	return tot;
}
int main()
{
	scanf("%d",&T);
	while(T--){
		init();
		scanf("%s",a+1);n=strlen(a+1);
		scanf("%d",&m);
		rep(i,0,(1<<n)-1){
			rep(j,0,3){
				sta[i][j]=work(i,s[j]);
			}
		}
		f[0][0]=1;
		rep(i,1,m){
			rep(S,0,(1<<n)-1){
				rep(j,0,3){
					f[i][sta[S][j]]=(f[i][sta[S][j]]+f[i-1][S])%mod;
				}
			}
		}
		rep(S,0,(1<<n)-1)
		ans[__builtin_popcount(S)]=(ans[__builtin_popcount(S)]+f[m][S])%mod;
		rep(i,0,n)printf("%d\n",ans[i]);
	}
	return 0;
	
}

 

相关文章:

  • 2022-12-23
  • 2019-03-22
  • 2021-11-06
  • 2021-10-26
  • 2021-10-29
  • 2021-10-14
猜你喜欢
  • 2022-01-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-01
  • 2021-06-29
  • 2021-06-08
相关资源
相似解决方案