传送阵:http://acm.hdu.edu.cn/showproblem.php?pid=4899

题目大意:给定一个DNA序列,求有多少长度为m的序列与该序列的最长公共子序列长度为0,1...|S|;

分析:
我们可以考虑对于求两个串的最长公共子序列的dp:f[i,j]代表第一个串到了i,第二个串到了j的最长公共子序列。对于两个串来说,如果数组f[i]是完全一样的,则它们对后面的影响也是完全一样的,所以我们可以设2维状态:F[i,j]代表我们当前以及到了i,f[i]的状态为j的方案数,但是这样设状态第二维有10^10。我们发现一个f[i,j]一定不会比f[i,j-1]小,且最多比f[i,j-1]多1,所以我们在设状态的时候可以取个差值,就变成2^10了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 20
#define mod 1000000007
char dic[]="ACTG";
int add[<<][];
int dp[][<<];
int pre[maxn],lcs[maxn],ans[maxn];
char s[maxn];
int T,n,m;
void Add(int &x,int y){
x+=y;
if(x>=mod)x-=mod;
}
void init(){
scanf("%s",s+);n=strlen(s+);
memset(add,,sizeof(add));
memset(dp,,sizeof(dp));
for(int state=;state<(<<n);++state){
pre[]=;
for(int i=;i<=n;++i)pre[i]=pre[i-]+((state>>(i-))&);
for(int k=;k<;++k){
for(int i=;i<=n;++i)
if(s[i]==dic[k])lcs[i]=pre[i-]+;
else lcs[i]=max(lcs[i-],pre[i]);
int &t=add[state][k];
for(int i=;i<=n;++i)
t|=((lcs[i]!=lcs[i-])<<(i-));
}
}
}
int get(int x){
int s=;
while(x){
s+=x&;
x>>=;
}return s;
}
void work(){
scanf("%d",&m);
int *now=dp[],*next=dp[];
memset(next,,(<<n)*sizeof(int));
next[]=;
for(int i=;i<=m;++i){
swap(now,next);
memset(next,,(<<n)*sizeof(int));
for(int state=;state<(<<n);++state)
if(now[state])
for(int k=;k<;++k)
Add(next[add[state][k]],now[state]);
}
memset(ans,,sizeof(ans));
for(int i=;i<(<<n);++i)
Add(ans[get(i)],next[i]);
for(int i=;i<=n;++i)
printf("%d\n",ans[i]);
}
int main(){
scanf("%d",&T);
while(T--){
init();
work();
}
return ;
}

hdu 4899 Hero meet devil的更多相关文章

  1. 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 ...

  2. HDU 4899 Hero meet devil (状压DP, DP预处理)

    题意:给你一个基因序列s(只有A,T,C,G四个字符,假设长度为n),问长度为m的基因序列s1中与给定的基因序列LCS是0,1......n的有多少个? 思路:最直接的方法是暴力枚举长度为m的串,然后 ...

  3. DP套DP HDOJ 4899 Hero meet devil(国王的子民的DNA)

    题目链接 题意: 给n长度的S串,对于0<=i<=|S|,有多少个长度为m的T串,使得LCS(S,T) = i. 思路: 理解的不是很透彻,先占个坑. #include <bits/ ...

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

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

  5. 【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 ...

  6. BZOJ3864 & HDU4899:Hero meet devil——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3864 http://acm.hdu.edu.cn/showproblem.php?pid=4899 ...

  7. bzoj3864: Hero meet devil

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

  8. BZOJ3864: Hero meet devil(dp套dp)

    Time Limit: 8 Sec  Memory Limit: 128 MBSubmit: 397  Solved: 206[Submit][Status][Discuss] Description ...

  9. BZOJ3864: Hero meet devil【dp of dp】

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

随机推荐

  1. jQuery视差滚动插件,(附原理分析,调用方法)

    演示地址:http://www.jq22.com/jquery-info1799 jquery插件,使用验证过可用. 分析源代码后总结原理:设置background样式为fixed,判断浏览器滚动距离 ...

  2. js 的复制和引用 (传值和传址)

    复制(传值-实参):  基本类型赋值.函数参数 引用(传址-形参):  对象.数组.函数

  3. EL操作 web 对象的常用方法

    11个常见的web对象 pageScope :获得pageContext对象中存的数据 requestScope :获得request对象中存的数据 sessionScope :获得session对象 ...

  4. 用Myeclipse,包名上有小红叉(表示错误),但是包中的类都是没有错误的+struts2继承ActionSupport时package前面出错

    使用Eclipse 进行项目开发,在实现类中的方法前面如果添加@Override就提示“Multiple markers at this line”的错误,问题描述如下 应该是JDK版本的问题, 右击 ...

  5. Swift - UITableView里的cell底部分割线左侧靠边

    override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, ...

  6. jsf初学解决faces 中文输入乱码问题

    中文乱码,貌似在java里很常见,请看简单代码: 很简单的faces <div class="td-block"> <h:outputLabel class=&q ...

  7. Java和C#下的参数验证

    参数的输入和验证问题是开发时经常遇到的,一般的验证方法如下: public bool Register(string name, int age) { if (string.IsNullOrEmpty ...

  8. Android 笔记 day3

    短信发送器 电脑弱爆了,开第二个emulater要10min!!! 顺便学会查看API文档 package com.example.a11; import java.util.*; import an ...

  9. MVC 好记星不如烂笔头之 ---> 页面压缩GIP

    public class BaseController : Controller { /// <summary> /// Called before the action method i ...

  10. HTTP POST请求报文格式分析与Java实现文件上传

    时间 2014-12-11 12:41:43  CSDN博客 原文  http://blog.csdn.net/bboyfeiyu/article/details/41863951 主题 HTTPHt ...