bzoj3864次元联通们

第一次写dp of dp (:з」∠) 不能再颓废啦

考虑最长匹配序列匹配书转移

由于dp[i][j]的转移可由上一行dp[i-1][j-1],dp[i-1][j],dp[i][j-1]得来

把dp[i]差分,得到一个01串

就可以用rans[s][ch]表示在状态s的dp数组后面接字符ch可以转移到的状态

枚举该转移就好了QAQ

/**************************************************************
Problem: 3864
Language: C++
Result: Accepted
Time:4208 ms
Memory:2356 kb
****************************************************************/
//f[i]为之前前i项的LCS,g[i]为添加字母之后的
//g数组的差值就是下一次匹配转移到的状态
#include<cstdio>
#include<cstring>
#include<algorithm>
using std::memset;
const int mod= 1000000007;
inline int read() {
int x=0,f=1;
char c=getchar() ;
while(c<'0'||c>'9') {
if(c=='-')f=-1;
c=getchar();
}
while(c<='9'&&c>='0') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}const int maxn = 16;
int cnt,m,n;
char a[40];
int tans[1<<maxn][4];
int ans[2][1<<maxn],f[maxn],g[maxn];
int is[maxn];
int s[maxn];
void network(int maxx) {
int now=1;
memset(ans,0,sizeof ans);
ans[0][0]=1;
for(int i=1;i<=m;++i,now=i&1) {
for(int j=0;j<4;++j)
for(int e=0;e<maxx;++e)
ans[now][tans[e][j]]=(ans[now][tans[e][j]]+ans[now^1][e])%mod;
memset(ans[now^1],0,sizeof ans[now^1]);
//now=i&1;
}
now^=1;
memset(is,0,sizeof is);
for(int i=0;i<maxx;++i) is[__builtin_popcount(i)]=(is[__builtin_popcount(i)]+ans[now][i])%mod;
for(int i=0;i<=n;++i)printf("%d\n",is[i]);
}
void solve(int m,int n) {
int maxx=1<<n;
for(int i=0;i<maxx;++i) {
f[0]=i&1;
for(int j=1;j<n;++j) f[j]=((i>>j)&1)+f[j-1];
for(int j=0;j<4;++j) {
memset(g,0,sizeof g);
for(int e=0;e<n;++e) {
if(s[e]==j) g[e] = f[e-1]+1;//最后一位与原来匹配
else g[e]=std::max(g[e-1],f[e]);
}
int tmp = 0;
for(int e=0;e<n;++e)
if(g[e]==g[e-1]+1)
tmp|=1<<e;
tans[i][j]=tmp;
}
}
network(maxx);
}
int main() {
cnt=read();
for(int i=1;i<=cnt;++i) {
scanf("%s",a);
m=read();n=strlen(a);
for(int i=0;i<n;++i)
if(a[i]=='A') s[i]=0;
else if(a[i]=='T') s[i]=1;
else if(a[i]=='C') s[i]=2;
else if(a[i]=='G') s[i]=3;
solve(m,n);
}
return 0;
}

bzoj 3864: Hero meet devil的更多相关文章

  1. bzoj 3864: Hero meet devil [dp套dp]

    3864: Hero meet devil 题意: 给你一个只由AGCT组成的字符串S (|S| ≤ 15),对于每个0 ≤ .. ≤ |S|,问 有多少个只由AGCT组成的长度为m(1 ≤ m ≤ ...

  2. BZOJ 3864 Hero meet devil 超详细超好懂题解

    题目链接 BZOJ 3864 题意简述 设字符集为ATCG,给出一个长为\(n(n \le 15)\)的字符串\(A\),问有多少长度为\(m(m \le 1000)\)的字符串\(B\)与\(A\) ...

  3. bzoj 3864: Hero meet devil(dp套dp)

    题面 给你一个只由\(AGCT\)组成的字符串\(S (|S| ≤ 15)\),对于每个\(0 ≤ .. ≤ |S|\),问 有多少个只由\(AGCT\)组成的长度为\(m(1 ≤ m ≤ 1000) ...

  4. BZOJ 3864 Hero meet devil (状压DP)

    最近写状压写的有点多,什么LIS,LCSLIS,LCSLIS,LCS全都用状压写了-这道题就是一道状压LCSLCSLCS 题意 给出一个长度为n(n<=15)n(n<=15)n(n< ...

  5. BZOJ 3864 Hero Meets Devil

    题目大意 给定一个由AGCT组成的串\(t\), 求对于所有的\(L \in [1, |t|]\), 有多少个由AGCT组成的串\(s\)满足\(LCS(s, t) = L\). Solution 传 ...

  6. 【BZOJ3864】Hero meet devil DP套DP

    [BZOJ3864]Hero meet devil Description There is an old country and the king fell in love with a devil ...

  7. bzoj千题计划241:bzoj3864: Hero meet devil

    http://www.lydsy.com/JudgeOnline/problem.php?id=3864 题意: 给你一个DNA序列,求有多少个长度为m的DNA序列和给定序列的LCS为0,1,2... ...

  8. HDU 4899 Hero meet devil(状压DP)(2014 Multi-University Training Contest 4)

    Problem Description There is an old country and the king fell in love with a devil. The devil always ...

  9. bzoj3864: Hero meet devil

    Description There is an old country and the king fell in love with a devil. The devil always asks th ...

随机推荐

  1. tarjan算法求LCA

    tarjan算法求LCA LCA(Least Common Ancestors)的意思是最近公共祖先,即在一棵树中,找出两节点最近的公共祖先. 这里我们使用tarjan算法离线算法解决这个问题. 离线 ...

  2. Hibernate 查询方法

    1.简单查询: public User select(User user) { User newUser; try { newUser = (User) session.get(User.class, ...

  3. Java Web Action DAO Service层次理解

    参考来源:http://blog.csdn.net/inter_peng/article/details/41021727 1. Action/Service/DAO简介: Action是管理业务(S ...

  4. Spring 事件机制

    通过模拟邮件的发送,说明Spring的事件监听机制 事件类 package org.zln.module_chapter2.event; import org.springframework.cont ...

  5. hdu 1242 Rescue (BFS)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  6. 洛谷 P3224 [HNOI2012]永无乡 解题报告

    P3224 [HNOI2012]永无乡 题目描述 永无乡包含 \(n\) 座岛,编号从 \(1\) 到 \(n\) ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 \(n\) 座岛排名,名次用 ...

  7. SICAU-OJ: 数字游戏

    数字游戏 题意:给出一个长度为n的数字,然后抹去k个数,使得剩下的数最大. 题解: 贪心的思想:让答案串中每一位尽可能大. 我们肯定要用完这k次的,假设有一个答案字符串ans,我们现在遍历给出的串,假 ...

  8. 牛客小白月赛4——H-相邻的糖果

    链接:https://www.nowcoder.com/acm/contest/134/H来源:牛客网 题目描述 有n个盒子摆成一排,每个盒子内都有ai个糖果. 现在你可以执行以下操作: ·你可以选择 ...

  9. HDU 1863 畅通工程 -Kruskal模版

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  10. myeclipse maven web项目配置

    启用maven:window-->preference-->MyEclipse-->Maven4MyEclipse, 勾选复选框(Enable Mave4MyEclipse feat ...