bzoj 3864 Hero meet devil

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值