Problem Description
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.

After the ring has been destroyed, the devil doesn't feel angry, and she is attracted by z*p's wisdom and handsomeness. So she wants to find z*p out.

But what she only knows is one part of z*p's DNA sequence S leaving on the broken ring.

Let us denote one man's DNA sequence as a string consist of letters from ACGT. The similarity of two string S and T is the maximum common subsequence of them, denote by LCS(S,T).

After some days, the devil finds that. The kingdom's people's DNA sequence is pairwise different, and each is of length m. And there are 4^m people in the kingdom.

Then the devil wants to know, for each 0 <= i <= |S|, how many people in this kingdom having DNA sequence T such that LCS(S,T) = i.

You only to tell her the result modulo 10^9+7.

 
Input
The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains a string S. the second line contains an integer m.

T<=5
|S|<=15. m<= 1000.

 
Output
For each case, output the results for i=0,1,...,|S|, each on a single line.
 
题目大意:字符集中只有“ATCG”四种字符,现在给你一个字符串S。问长度为m的所有字符串T中,最长公共子序列lcs(S, T) = i,其中 i 取遍0~|S|。|S|为S的长度。
思路:令state = lcs(S, T),其中state为lcs在S中的位置的信息再状态压缩后的值。总之这是一个状态压缩DP。
如果用dp[i][state]来表示lcs(S, T[1..i])的T[1..i]的数量,那么计算出来的结果会有重复导致无法得出正确的结果。
定义newlcs(S, T) = lcs(S, T)的最小的state。
举个栗子:S = AATTCCGG
那么对于T = ATCG
newlcs(S, T) = pos[0, 2, 4, 6],有且只有这一个解(从0开始计数)
这样,用dp[i][state]来表示newlcs(S, T[1..i])的T[1..i]的数量。那么对于每个T[1..i],有且只有一个dp[i][state]和它对应,其中state = newlca(S, T[1..i])。
那么就可以花O(2^n*n*4)的时间来预处理出每个state加上一个字符得到的新state。
再花O(m*2^n*4)的时间用滚动数组做状态压缩DP。
再统计一下答案,解决。
PS:代码里面__builtin_popcount()是G++内置函数,用于求一个数的二进制中1的个数。
 
代码(1281MS):
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = ;
const int MOD = 1e9 + ; char dic[] = "ACTG";
int add[ << ][];
int dp[][ << ];
int pre[MAXN], lcs[MAXN], ans[MAXN];
char s[MAXN];
int T, n, m; inline void update_add(int &a, int b) {
a += b;
if(a >= MOD) a -= MOD;
} void init() {
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 - ));
}
}
} void solve() {
int *now = dp[], *next = dp[];
memset(next, , ( << n) * sizeof(int));
next[] = ;
for(int _ = ; _ < m; ++_) {
swap(now, next);
memset(next, , ( << n) * sizeof(int));
for(int state = ; state < ( << n); ++state) if(now[state])
for(int k = ; k < ; ++k)
update_add(next[add[state][k]], now[state]);
}
memset(ans, , sizeof(ans));
for(int state = ; state < ( << n); ++state) {
update_add(ans[__builtin_popcount(state)], next[state]);
}
for(int i = ; i <= n; ++i)
printf("%d\n", ans[i]);
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%s%d", s + , &m);
n = strlen(s + );
init();
solve();
}
}

HDU 4899 Hero meet devil(状压DP)(2014 Multi-University Training Contest 4)的更多相关文章

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

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

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

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

  3. hdu 4899 Hero meet devil

    传送阵:http://acm.hdu.edu.cn/showproblem.php?pid=4899 题目大意:给定一个DNA序列,求有多少长度为m的序列与该序列的最长公共子序列长度为0,1...|S ...

  4. HDU 6149 Valley Numer II 状压DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意:中文题目 解法:状压DP,dp[i][j]代表前i个低点,当前高点状态为j的方案数,然后枚 ...

  5. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

  6. HDU 1074 Doing Homework(状压DP)

    第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...

  7. HDU 4906 Our happy ending (状压DP)

    HDU 4906 Our happy ending pid=4906" style="">题目链接 题意:给定n个数字,每一个数字能够是0-l,要选当中一些数字.然 ...

  8. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

  9. HDU 4568 Hunter 最短路+状压DP

    题意:给一个n*m的格子,格子中有一些数,如果是正整数则为到此格子的花费,如果为-1表示此格子不可到,现在给k个宝藏的地点(k<=13),求一个人从边界外一点进入整个棋盘,然后拿走所有能拿走的宝 ...

随机推荐

  1. autowire异常的三个情况

    2010-3-11 16:06:00 net.sf.ehcache.config.ConfigurationFactory parseConfiguration 警告: No configuratio ...

  2. asr,tts,vsr

    http://max.book118.com/html/2014/0814/9432056.shtm   ASR技术的基础主要是信号处理和概率模型. 信号处理技术 语音信号处理 谱分析   基于时间的 ...

  3. C++字符数字的编码(Encode)与解码(Decode)

    在日常应用中,我们常用结构体或者类来存储一条信息,这种方式很方便,但是不利于数据的传输.例如在网络编程中,我们需要将结构中的数据转化为字节流才能进行传输,我们可以利用memcpy强行将结构化的数据转化 ...

  4. Search in Rotated Sorted Array I

    Search in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to you befo ...

  5. LightOj1056 - Olympics(简单数学题)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1056 题意:已知体育场的形状是由一个矩形+两边的两个部分组成,两边的两个部分是属于同一 ...

  6. Sort---hdu5884(优先队列+二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 题意:有n个有序序列,每个序列有ai个元素,现在有一个程序每次可以归并最多k个序列,最终把所有的 ...

  7. JMeter学习-003-JMeter与LoadRunner的异曲同工

    本节主要对 JMeter 与 LoadRunner 的优缺点进行概要的总结,若有不足之处,敬请指正,不胜感激! 同时,我也不得不承认,在对 JMeter 和 LoadRunner 进行比较时,我个人的 ...

  8. angularJs:双向数据绑定

    示例1 <!DOCTYPE html> <html ng-app> <head> <meta charset="UTF-8" /> ...

  9. 好网站:字体转换器在线转换 http://www.diyiziti.com/

    偶然间发现这个网站,它可以将文字设置字体并做成图片,也有很多好的字体下载如瘦金体. 下载下来的ttf文件在win7里可以右键点击install,在vista和xp里可以直接拷贝到window\font ...

  10. (转)Eclipse New Server 【无法输入server name】

    I was trying to add Tomcat 7 in my Eclipse in Ubuntu. When I click “Add new server” in Eclipse and s ...