bzoj 3864 Hero meet devil 动态动态规划

在一个古老的国度里,国王被一位外表可爱的恶魔所吸引。恶魔希望通过比对DNA序列找到一位名叫z*p的智者。面对仅有部分DNA序列的情况,恶魔需要计算王国中每个人与目标序列的不同匹配度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.elijahqi.win/archives/3265
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

非常有趣的一题

动态动态规划 省选考的板子题

考虑普通lcs是怎么做的

lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1],(lcs[i-1][j-1]+1)*(t[i]==s[j]))

考虑将i设置成第一个字符串的

然后j设置成 题目中S串的第j个位置

那么考虑我这个i属于的串是一个固定的串 那么考虑求lcs的时候是怎么求的

我会得到一个lcs矩阵 表示 lcs[i][j] 并且我知道 相邻的j-1~j只会最多+1 那么我们考虑 将i固定之后 获得lcs[i]生成的一个数组 然后对这个数组进行差分 那么可以得到一个0,1的序列 那么 就可以考虑把他们状压起来

于是首先预处理trans[s][owo]表示我当前匹配在第i-1个位置 我lcs第二维的差分状态为s 的情况下我再在s串中添加owo这个字母 新生成的第二维差分状态是多少

然后设dp[i][s]表示第一个串中我匹配到第i个位置 我lcs第二维状态差分之后是s 我的方案数是多少

然后最后统计的时候就循环所有状态数一下1的个数就是我lcs的个数 统计之后输出即可

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
char s1[20],w[1000];
int n,S,m,trans[65536][4],h[20],g[20],dp[2][65536],bin[20],T,ans[20];
inline int make(int *h){
    int s=0;
    for (int i=1;i<=n;++i){
        s|=(h[i]-h[i-1])*bin[i-1];
    }return s;
}
inline void make1(int *g,int s){
    g[0]=0;
    for (int i=1;i<=n;++i){
        g[i]=g[i-1];g[i]+=(s&bin[i-1])>0;
    }
}
inline void pre(){
    for (int owo=0;owo<=3;++owo){
        for (int s=0;s<=S;++s){
            make1(g,s);memset(h,0,sizeof(h));
            for (int i=1;i<=n;++i){
                h[i]=max(h[i-1],max(g[i],(s1[i]==w[owo])*(g[i-1]+1)));
            }trans[s][owo]=make(h);
        }
    }
}
inline void inc(int &x,int v){x=x+v>=mod?x+v-mod:x+v;}
inline int bc(int s){int tmp=0;
    for (int i=0;i<n;++i) tmp+=(bin[i]&s)>0;return tmp;
}
int main(){
    freopen("bzoj3864.in","r",stdin);
    scanf("%d",&T);w[0]='A';w[1]='T';w[2]='C';w[3]='G';
    for (int i=0;i<=15;++i) bin[i]=1<<i;
    while(T--){
        scanf("%s",s1+1);n=strlen(s1+1);scanf("%d",&m);S=(1<<n)-1;pre();
        memset(dp,0,sizeof(dp));int pre=0,now=1;dp[pre][0]=1;
        for (int i=0;i<m;++i){
            memset(dp[now],0,sizeof(dp[now]));
            for (int s=0;s<=S;++s){
                if (!dp[pre][s]) continue;
                for (int owo=0;owo<=3;++owo){
                    int y=trans[s][owo];
                    inc(dp[now][y],dp[pre][s]);
                }
            }now^=1;pre^=1;
        }memset(ans,0,sizeof(ans));
        for (int i=0;i<=S;++i) inc(ans[bc(i)],dp[pre][i]);
        for (int i=0;i<=n;++i) printf("%d\n",ans[i]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值