BZOJ 3864: Hero meet devil Dp套Dp

title

BZOJ 3864
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 zp's wisdom and handsomeness. So she wants to find zp 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

Source

By WJMZBMR

analysis

考虑计算 \(LCS\)\(DP\) 过程,\(f[i][j]\) 表示 \(T\) 串的前 \(i\) 项与 \(S\) 串的前 \(j\) 项的 \(LCS\),则

  • \(T[i]==S[j]\),则\(f[i][j]=f[i-1][j-1]+1\)
  • 否则\(f[i][j]=max(f[i-1][j],f[i][j-1])\)

对于固定的 \(i\)\(f[i][j]\) 只可能为 \(f[i][j-1]\)\(f[i][j-1]+1\),把这个差值用二进制表示成状态。

先预处理出每个状态后面加了一个字符后会到达什么状态,然后进行状压DP即可。

时间复杂度\(O(m2^n)\)

code

#include<bits/stdc++.h>
using namespace std;
const int maxn=15,mod=1e9+7;
 
char buf[1<<15],*fs,*ft;
inline char getc() { return (ft==fs&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),ft==fs))?0:*fs++; }
template<typename T>inline void read(T &x)
{
    x=0;
    T f=1, ch=getchar();
    while (!isdigit(ch) && ch^'-') ch=getchar();
    if (ch=='-') f=-1, ch=getchar();
    while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
    x*=f;
}
 
template<typename T>inline void write(T x)
{
    if (!x) { putchar('0'); return ; }
    if (x<0) putchar('-'), x=-x;
    T num=0, ch[20];
    while (x) ch[++num]=x%10+48, x/=10;
    while (num) putchar(ch[num--]);
}
 
typedef int iarr[maxn+1];
iarr a,f,g,ans;
int v[1<<maxn][4];
int F[2][1<<maxn];
char ch[maxn+1];
int main()
{
    int T;read(T);
    while (T--)
    {
        scanf("%s",ch+1);
        int n=strlen(ch+1);
        int m;read(m);
        for (int i=1; i<=n; ++i)
        {
            if (ch[i]=='A') a[i]=0;
            if (ch[i]=='G') a[i]=1;
            if (ch[i]=='T') a[i]=2;
            if (ch[i]=='C') a[i]=3;
        }
        for (int i=0; i<(1<<n); ++i)
        {
            for (int j=0; j<n; ++j) f[j+1]=f[j]+(i>>j&1);
            for (int j=0; j<4; ++j)
            {
                for (int k=1; k<=n; ++k)
                    if (a[k]==j) g[k]=f[k-1]+1;
                    else g[k]=max(f[k],g[k-1]);
                v[i][j]=0;
                for (int k=1; k<=n; ++k)
                    if (g[k]>g[k-1]) v[i][j]|=1<<k-1;
            }
        }
        for (int i=0; i<(1<<n); ++i) F[0][i]=0;
        int x=0;F[0][0]=1;
        for (int i=0; i<m; ++i,x^=1)
        {
            for (int j=0; j<(1<<n); ++j) F[x^1][j]=0;
            for (int j=0; j<(1<<n); ++j) if (F[x][j])
                for (int k=0; k<4; ++k) F[x^1][v[j][k]]=(F[x^1][v[j][k]]+F[x][j])%mod;
        }
        for (int i=0; i<=n; ++i) ans[i]=0;
        for (int i=0,v; i<(1<<n); ++i) v=__builtin_popcount(i),ans[v]=(ans[v]+F[x][i])%mod;
        for (int i=0; i<=n; ++i) write(ans[i]),puts("");
    }
    return 0;
}

转载于:https://www.cnblogs.com/G-hsm/p/11318550.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值