BZOJ5336:[TJOI2018]游园会——题解

时间:2023-03-09 04:49:55
BZOJ5336:[TJOI2018]游园会——题解

https://www.lydsy.com/JudgeOnline/problem.php?id=5336

https://www.luogu.org/problemnew/show/P4590

小豆参加了NOI的游园会,会场上每完成一个项目就会获得一个奖章,奖章  只会是N, O, I的字样。在会场上他收集到了K个奖章组成的串。
兑奖规则是奖章串和兑奖串的最长公共子序列长度为小豆最后奖励的等级。
现在已知兑奖串长度为N,并且在兑奖串上不会出现连续三个奖章为NOI,即奖章中不会出现子串NOI。
现在小豆想知道各个奖励等级会对应多少个不同的合法兑奖串。

BZOJ3864 & HDU4899:Hero meet devil大致一样,至于那个多出来的限制想怎么做就怎么做(包括我自己曾想过一个枚举的算法)。

一个很妙的做法是多记录当前的dp状态的末尾已经到达了"NOI"状态的第几位,然后转移即可。

#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int p=1e9+;
const int N=;
const int M=;
inline int read(){
int X=,w=;char ch=;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
int s[N+];
int m,n,ans[N+],f[][<<N][],last[<<N],trans[<<N][],mp[][];
inline void readstring(){
int cnt=;char ch=;
while(ch<'A'||ch>'Z')ch=getchar();
while(ch>='A'&&ch<='Z'){
if(ch=='N')s[++cnt]=;if(ch=='O')s[++cnt]=;
if(ch=='I')s[++cnt]=;
ch=getchar();
}
}
void init(){
static int d[N+],g[N+];
for(int i=;i< <<n;i++){
if(i)last[i]=last[i^(i&-i)]+;
for(int j=;j<n;j++)d[j+]=d[j]+(bool)(i&(<<j));
for(int k=;k<;k++){
for(int j=;j<=n;j++){
g[j]=max(g[j-],d[j]);
if(s[j]==k)g[j]=max(g[j],d[j-]+);
}
trans[i][k]=;
for(int j=;j<n;j++)
if(g[j+]-g[j])trans[i][k]|=<<j;
}
}
}
int main(){
m=read(),n=read();
readstring();
init();
f[][][]=;int now=;
mp[][]=;mp[][]=;mp[][]=;mp[][]=;
mp[][]=;mp[][]=;mp[][]=;mp[][]=;
for(int i=;i<=m;i++){
memset(f[now],,sizeof(f[now]));
for(int j=;j< <<n;j++){
for(int l=;l<;l++){
for(int k=;k<;k++){
if(k==&&l==)continue;
(f[now][trans[j][k]][mp[l][k]]+=f[now^][j][l])%=p;
}
}
}
now^=;
}
for(int i=;i< <<n;i++)
for(int j=;j<;j++)
(ans[last[i]]+=f[now^][i][j])%=p;
for(int i=;i<=n;i++)printf("%d\n",(ans[i]+p)%p);
return ;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者:luyouqi233。               +

+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++