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]。

第一眼看过去想的是 dp[i][j][k] 表示T串前i个字符,S串前j个字符,LCS长度为k的方案数…结果发现LCS没法算,导致方程无法转移…

于是%了一发题解。

考虑LCS的状态转移方程
dp[i][j]=max(dp[i1][j],dp[i][j1],dp[i1][j1]+1)

把dp数组以矩阵的形式写出来,可以发现这个矩阵有同一行相邻两项最多差1这个奇葩的性质,又观察到S串最多15个,莫名其妙想到了状压。

所以一行的状态可以用一个01串表示,1代表该位置比前一个位置大1,0表示等于前一个位置。然后这样我们就可以通过朴素的LCS算法从1行转移到下一行。搞出来转移矩阵trans[i][k]:当前LCS状态为i的情况下T串填字符k能到的状态。

预处理出来就可以做一个普通的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;}
原创粉丝点击