P4590 [TJOI2018]游园会 dp套dp + 状态机
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                P4590 [TJOI2018]游园会 dp套dp + 状态机
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                傳送門
文章目錄
- 題意:
- 思路:
題意:
給你一個長度為nnn的串sss,其只包含NOINOINOI三個字母,給你一個mmm,代表ttt串的長度,ttt串包含NOINOINOI三個字母但是不存在三個連續的NOINOINOI子串,輸出n+1n+1n+1行,第iii行輸出有多少合法的ttt串與sss串的lcslcslcs為iii。
m≤1000,n≤15m\le1000,n\le15m≤1000,n≤15
思路:
也是一個dpdpdp套dpdpdp的題,但是多了一個限制就是不存在連續的NOINOINOI串,這個我們只需要在最后轉移的時候加一個狀態,代表當前連續串的長度是多少,NONONO就是長度為222。轉移的時候避免掉不合法狀態轉移,對于狀態機的構建與上一個題相同,就不多說了。
復雜度O(3?2?m?2n)O(3*2*m*2^n)O(3?2?m?2n)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native") //#pragma GCC optimize(2) #include<cstdio> #include<iostream> #include<string> #include<cstring> #include<map> #include<cmath> #include<cctype> #include<vector> #include<set> #include<queue> #include<algorithm> #include<sstream> #include<ctime> #include<cstdlib> #include<random> #include<cassert> #define X first #define Y second #define L (u<<1) #define R (u<<1|1) #define pb push_back #define mk make_pair #define Mid ((tr[u].l+tr[u].r)>>1) #define Len(u) (tr[u].r-tr[u].l+1) #define random(a,b) ((a)+rand()%((b)-(a)+1)) #define db puts("---") using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); } //void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); } //void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII;const int N=110,mod=1e9+7,INF=0x3f3f3f3f; const double eps=1e-6;int n,m; int a[N]; LL f[2][1<<16][4],ans[N]; int ne[5][1<<16]; int g1[N],g2[N]; char s[N];int get(char ch) {if(ch=='N') return 1;if(ch=='O') return 2;if(ch=='I') return 3;while(1);return -1; }void init() {memset(ne,0,sizeof(ne));for(int i=0;i<1<<n;i++) {for(int j=1;j<=n;j++) g2[j]=g2[j-1]+(i>>(j-1)&1);for(int k=1;k<=3;k++) {for(int j=1;j<=n;j++) {g1[j]=max(g1[j-1],g2[j]);if(a[j]==k) g1[j]=max(g1[j],g2[j-1]+1);}int state=0;for(int i=0;i<n;i++) if(g1[i+1]-g1[i]) state+=1<<i;ne[k][i]=state;}} }void solve() {memset(f[0],0,sizeof(f[0]));memset(f[1],0,sizeof(f[1]));int now=0;f[now][0][0]=1; now^=1;for(int i=1;i<=m;i++,now^=1) {memset(f[now],0,sizeof(f[now]));for(int len=0;len<=2;len++) {for(int k=1;k<=3;k++) {int to;if(len==1&&k==2) to=2;else if(len==2&&k==3) continue;else if(k==1) to=1;else to=0;for(int j=0;j<1<<n;j++) {(f[now][ne[k][j]][to]+=f[now^1][j][len])%=mod;}}}}now^=1;for(int i=0;i<=n;i++) ans[i]=0;for(int i=0;i<1<<n;i++) {for(int j=0;j<=2;j++) {(ans[__builtin_popcount(i)]+=f[now][i][j])%=mod;}} }int main() { // ios::sync_with_stdio(false); // cin.tie(0);scanf("%d%d%s",&m,&n,s+1); for(int i=1;i<=n;i++) a[i]=get(s[i]);init(); solve();for(int i=0;i<=n;i++) printf("%lld\n",ans[i]%mod);return 0; } /**/ 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的P4590 [TJOI2018]游园会 dp套dp + 状态机的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 马齿苋怎么煮水治皮肤
- 下一篇: 为什么吃中药脸上长痘
