1. 程式人生 > >BZOJ 5336: [TJOI2018]party

BZOJ 5336: [TJOI2018]party

brush scanf zoj tin 狀壓 16px std font esp

狀壓最長公共子序列的DP數組,一維最多K(15)個數,且相鄰兩個數的差不超過1,2^15種狀態,預處理轉移

#include<cstdio>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
int n,K,now[25],G[25],To[50005][3],F[2][50005][3],ED[50005],ANS[25],S[25];
char s[1000005];
void dfs(int t,int s){
	if (t>K){
		for (int i=1; i<=K; i++) {
			now[i]=now[i-1];
			if (s&(1<<i-1)) now[i]++;
		}
		ED[s]=now[K];
		for (int to=0; to<3; to++){
			for (int i=1; i<=K; i++) G[i]=max(max(G[i-1],now[i]),now[i-1]+(to==S[i]));
			for (int i=K; i>=1; i--) (To[s][to]<<=1)|=(G[i]-G[i-1]);
		}
		return;
	}
	dfs(t+1,s<<1);
	dfs(t+1,s<<1|1);
}
int main(){
	scanf("%d%d",&n,&K);
	scanf("%s",s+1);
	for (int i=1; i<=K; i++){
		if (s[i]==‘N‘) S[i]=0;
		else if (s[i]==‘O‘) S[i]=1;
		else S[i]=2;
	}
	dfs(1,0);
	F[0][0][0]=1;
	for (int i=0; i<n; i++){
		for (int pre=0; pre<(1<<K); pre++)
			for (int cas=0; cas<3; cas++)
				F[(i+1)%2][pre][cas]=0;
		for (int pre=0; pre<(1<<K); pre++)
			for (int cas=0; cas<3; cas++)
				if (F[i%2][pre][cas]){
					for (int to=0; to<3; to++){
						int now=To[pre][to],Tocas=cas;
						if (to==cas) Tocas++;
						else{
							if (to==0) Tocas=1;
							else Tocas=0;
						}
						if (Tocas==3) continue;
						(F[(i+1)%2][now][Tocas]+=F[i%2][pre][cas])%=mod;
					}
				}
	}
	for (int now=0; now<(1<<K); now++) 
		for (int cas=0; cas<3; cas++)
			(ANS[ED[now]]+=F[n%2][now][cas])%=mod;
	for (int i=0; i<=K; i++)
		printf("%d\n",ANS[i]);
	return 0;
}

  

BZOJ 5336: [TJOI2018]party