bzoj 3864 Hero meet devil
来源:互联网 发布:python 交易 行情平台 编辑:程序博客网 时间:2024/06/07 06:05
3864: Hero meet devil
Time Limit: 8 Sec Memory Limit: 128 MB
Submit: 253 Solved: 134
[Submit][Status][Discuss]
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.
Sample Input
1
GTC
10
Sample Output
1
22783
528340
497452
HINT
Source
By WJMZBMR
【分析】
DP套DP…
第一次做这么好玩的题[笑cry]。
第一眼看过去想的是
于是%了一发题解。
考虑LCS的状态转移方程
把dp数组以矩阵的形式写出来,可以发现这个矩阵有同一行相邻两项最多差1这个奇葩的性质,又观察到S串最多15个,莫名其妙想到了状压。
所以一行的状态可以用一个01串表示,1代表该位置比前一个位置大1,0表示等于前一个位置。然后这样我们就可以通过朴素的LCS算法从1行转移到下一行。搞出来转移矩阵
预处理出来就可以做一个普通的DP了。
真的好神奇QaQaQQQ
【代码】
//bzoj 3864 Hero meet devil#include<bits/stdc++.h>#define ll long long#define M(a) memset(a,0,sizeof a)#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;const int mod=1e9+7;const int mxn=1005;int n,m,T;char s[mxn],ch[5]={' ','A','G','C','T'};int dp[mxn][1<<15],trans[1<<15][5],f[25],g[25],cnt[1<<15],ans[25];inline void init(){ int i,j,k; M(trans),M(dp); fo(i,0,(1<<n)-1) { fo(j,1,n) f[j]=f[j-1]+(i&(1<<j-1)?1:0); cnt[i]=f[n]; fo(k,1,4) { fo(j,1,n) { g[j]=max(g[j-1],f[j]); if(ch[k]==s[j]) g[j]=max(g[j],f[j-1]+1); } fo(j,1,n) if(g[j]-g[j-1]>0) trans[i][k]|=(1<<j-1); } } dp[0][0]=1; fo(i,0,m-1) fo(j,0,(1<<n)-1) if(dp[i][j]) fo(k,1,4) dp[i+1][trans[j][k]]=(dp[i+1][trans[j][k]]+dp[i][j])%mod; fo(j,0,(1<<n)-1) ans[cnt[j]]=(ans[cnt[j]]+dp[m][j])%mod; fo(i,0,n) printf("%d\n",ans[i]);}int main(){ int i,j; scanf("%d",&T); while(T--) { M(ans),M(dp); scanf("%s%d",s+1,&m); n=strlen(s+1),init(); } return 0;}
- bzoj 3864 Hero meet devil
- [BZOJ 3864][HDU 4899]Hero meet devil(DP套DP)
- BZOJ 3864 Hero meet devil DP套DP
- BZOJ 3864: Hero meet devil【dp套dp
- [BZOJ 3864][HDU 4899]Hero meet devil(DP套DP)
- [DP套DP] BZOJ 3864 Hero meet devil
- BZOJ3864: Hero meet devil
- BZOJ3864 Hero meet devil
- HDU 4899 Hero meet devil
- hdu 4899 Hero meet devil
- hdu 4899 Hero meet devil
- hdu 4899 Hero meet devil(状压dp)
- [BZOJ3864]Hero meet devil(状压dp)
- BZOJ3864: Hero meet devil DP套DP
- [DP套DP] BZOJ3864: Hero meet devil
- 【bzoj 3864】Hero meets devil - DP套DP
- [HDOJ 4899] Hero meet devil [动态规划+状态压缩]
- HDU-4899 Hero meet devil(DP套DP)
- ubuntu 16.04内核源码编译
- EL隐藏对象
- Bash脚本条件判断和控制流结构
- 数据库笔记——Mysql、Oracle、Sqlserver || Redis、Memcached、mongoDB环境搭建
- pycaffe 搭建:常见问题解决办法
- bzoj 3864 Hero meet devil
- POJ 1845 Sumdiv 笔记
- 初识node 创建或者删除一个文件目录
- 在ATL项目中添加WTL界面
- 面试题25:二叉树中和为某一值的路径
- mFAST fast_type_gen流程详解
- 《使用python进行自然语言理解》学习笔记二
- 论android等平台回音消除解决方案
- 类似QQ空间,微信朋友圈,微博主页等,展示图片的九宫格控件