BZOJ3864: Hero meet devil(dp套dp)

Time Limit: 8 Sec  Memory Limit: 128 MB
Submit: 397  Solved: 206
[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.

1
GTC
10

1
22783
528340
497452

Source

$$lcs[i][j]=max \begin{cases} lcs[i-1][j-1]+1 & \text{if t[i]=s[j]} \\ lcs[i-1][j] \\ lcs[i][j-1] \end{cases}$$

$$f[i][ trans[sta][k] ] += f[i - 1][sta]$$

$$f[0][0] = 1$$

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int  MAXN = 1001, mod = 1e9 + 7;
char S[16], SS[] = {"ACGT"};
int a[16], f[MAXN][(1 << 15) + 2], trans[(1 << 15) + 2][5], N, Len, limit, ans[111];
int tmp[2][16];
int solve(int sta, int ch) {
int ret = 0;
memset(tmp, 0, sizeof(tmp));
for(int i = 0; i < N; i++) tmp[0][i + 1] = tmp[0][i] + ((sta >> i) & 1 );
for(int i = 1; i <= N; i++) {
int mx = 0;
if(a[i] == ch) mx = tmp[0][i - 1] + 1;
mx = max( max(mx, tmp[0][i]), tmp[1][i-1]);
tmp[1][i] = mx;
}
for(int i = 0; i < N; i++) ret += (1 << i) * (tmp[1][i + 1] - tmp[1][i]);
return ret;
}
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#endif
int QWQ;scanf("%d", &QWQ);
while(QWQ--) {
memset(f, 0, sizeof(f));memset(ans, 0, sizeof(ans));
scanf("%s", S + 1);
N = strlen(S + 1); limit = (1 << N) - 1;
for(int i = 1; i <= N; i++)
for(int j = 0; j < 4; j++)
if(S[i] == SS[j]){a[i] = j + 1;break;}
scanf("%d", &Len);
f[0][0] = 1;
for(int sta = 0; sta <= limit; sta++)
for(int j = 1; j <= 4; j++)
trans[sta][j] = solve(sta, j);
for(int i = 1; i <= Len; i++)
for(int sta = 0; sta <= limit; sta++)
for(int k = 1; k <= 4; k++)
f[i][ trans[sta][k] ] = (f[i][ trans[sta][k] ] + f[i - 1][sta]) % mod;
for(int sta = 0; sta <= limit; sta++)
ans[__builtin_popcount(sta)] = (ans[__builtin_popcount(sta)] + f[Len][sta]) % mod;
//这个函数是算出sta中1的个数
for(int i = 0; i <= N; i++)
printf("%d\n", ans[i] % mod);
}
return 0;
}

posted @ 2018-05-02 08:28  自为风月马前卒  阅读(903)  评论(0编辑  收藏  举报