bzoj3864 Hero meet devil

3864: Hero meet devil

Time Limit: 8 Sec  Memory Limit: 128 MB
Submit: 360  Solved: 189
[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

题目大意:给一个只包含AGCT的字符串和一个数字M,求有多少个长度为M的只包含AGCT的字符串与给定的字符串长度为i (0 ≤ i ≤ |S|).对于每个i分别输出答案.
分析:非常神奇的一道题.
   考虑一般的lcs转移方程:f[i][j] = max{f[i - 1][j],f[i][j - 1],f[i - 1][j - 1] + 1}.现在只考虑第一维.可以发现0 ≤ f[i] - f[i - 1] ≤ 1.
那么可以用一个二进制数来表示这个状态:第i位为1表示f[i + 1] - f[i] = 1,为0则表示f[i + 1] - f[i] = 0.
   怎么统计方案数?令f[i][j]表示字符串的前i位中,状态为j的方案数. 每次可以从f[i][j] 转移到 f[i + 1][next[j][k]]. next[j][k]表示状态为j的字符串后加一个字符k能够转移到的状态.
   如何得到next数组呢?令g[i]表示在给定的字符串的前i位中,向另一个字符串后插入了字符k后的LCS. g2[i]表示没有插入的. 先枚举状态i. 可以根据i中1的数量求得g2[i]的值.  接着枚举插入的字符k和字符串的长度j. g[j] = max{g[j - 1],g2[j]},如果s[j] == k,那么g[j] = max{g[j],g2[j - 1] + 1}(相当于匹配了一位). 这里处理得到的g数组就相当于一开始的f数组.根据g数组确定状态,next[i][k]就知道了,就能够转移了.
   挺难说清楚的,代码倒不是很长,比较容易读懂. 但是要注意一个细节:取模不要直接%,会T掉,正确的方法是检查要取模的数是不是大于模数,如果大于就减去模数. 这种做法只能优化只有加法取模的问题.
#include <cstdio>
#include <map>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

typedef long long ll;
int T,S,len;
const ll maxn = 1 << 16,mod = 1e9+7;
ll f[2][maxn],a[30],g[30],nextt[maxn][5],g2[30],maxx,ans[30];
char s[30];

map <char,ll> M;

void init()
{
    for (int i = 0; i <= maxx; i++)
    {
        memset(g2,0,sizeof(g2));
        for (int j = 1; j <= len; j++)
            g2[j] = g2[j - 1] + ((i >> (j - 1)) & 1);
        for (int k = 1; k <= 4; k++)
        {
            for (int j = 1; j <= len; j++)
            {
                g[j] = max(g[j - 1],g2[j]);
                if (k == a[j])
                    g[j] = max(g[j],g2[j - 1] + 1);
            }
            nextt[i][k] = 0;
            for (int j = 0; j < len; j++)
                if (g[j + 1] - g[j])
                    nextt[i][k] += (1 << j);
        }
    }
}

int cal(int x)
{
    int cnt = 0;
    while (x)
    {
        if (x & 1)
            cnt++;
        x >>= 1;
    }
    return cnt;
}

void solve()
{
    memset(f,0,sizeof(f));
    int now = 1,last = 0;
    f[0][0] = 1;
    for (int i = 1; i <= S; i++)
    {
        memset(f[now],0,sizeof(f[now]));
        for (int j = 0; j <= maxx; j++)
            for (int k = 1; k <= 4; k++)
            {
                f[now][nextt[j][k]] += f[last][j];
                if (f[now][nextt[j][k]] >= mod)
                    f[now][nextt[j][k]] -= mod;
            }
        swap(now,last);
    }
    memset(ans,0,sizeof(ans));
    for (int i = 0; i <= maxx; i++)
    {
        int temp = cal(i);
        ans[temp] += f[last][i];
        if (ans[temp] >= mod)
            ans[temp] -= mod;
    }
    for (int i = 0; i <= len; i++)
        printf("%lld\n",ans[i]);
}

int main()
{
    M['A'] = 1;
    M['C'] = 2;
    M['G'] = 3;
    M['T'] = 4;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%s",s + 1);
        len = strlen(s + 1);
        for (int i = 1; i <= len; i++)
            a[i] = M[s[i]];
        maxx = (1 << len) - 1;
        scanf("%d",&S);
        init();
        solve();
    }

    return 0;
}

 

posted @ 2018-03-02 14:15  zbtrs  阅读(316)  评论(0编辑  收藏  举报