BZOJ3864: Hero meet devil【dp of dp】

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


给你一个序列s,问长度为m的和s最长公共子序列是\([0,|s|]\)的串有多少个

思路

dp of dp

因为最长公共子序列的dp矩阵每一行相邻两项不会超过1,所以就可以用二进制状压

然后就先dp出每个状态加上一个字符的转移

然后上状压dp就可以了

注意dp数组的清零问题


#include<bits/stdc++.h>

using namespace std;

const int Mod = 1e9 + 7;
const int N = 16;
const int M = 1 << N;

int trans[M][4], bitcnt[M], dp[2][M];
int len, m, ans[N], f[N], g[N];
char s[N];

int add(int a, int b) {
  return (a += b) >= Mod ? a - Mod : a;
}

void solve() {
  scanf("%s%d", s + 1, &m);
  len = strlen(s + 1);
  for (int i = 1; i <= len; i++) {
    if (s[i] == 'A') s[i] = 0;
    if (s[i] == 'C') s[i] = 1;
    if (s[i] == 'G') s[i] = 2;
    if (s[i] == 'T') s[i] = 3;
  }
  for (int i = 0; i < (1 << len); i++) {
    for (int j = 1; j <= len; j++) {
      f[j] = f[j - 1] + ((i >> (j - 1)) & 1);
    }
    for (int j = 0; j < 4; j++) {
      for (int k = 1; k <= len; k++) {
        g[k] = max(g[k - 1], f[k]);
        if (s[k] == j) g[k] = max(g[k], f[k - 1] + 1);
      }
      trans[i][j] = 0;
      for (int k = 1; k <= len; k++) {
        trans[i][j] |= (g[k] - g[k - 1]) << (k - 1);
      }
    }
  }
  int ind = 0;
  for (int i = 0; i < (1 << len); i++) dp[ind][i] = 0; // **
  dp[ind][0] = 1;
  for (int i = 1; i <= m; i++) {
    ind ^= 1;
    for (int j = 0; j < (1 << len); j++) {
      dp[ind][j] = 0;
    }
    for (int j = 0; j < (1 << len); j++) if (dp[ind ^ 1][j]) {
      for (int k = 0; k < 4; k++) {
        dp[ind][trans[j][k]] = add(dp[ind][trans[j][k]], dp[ind ^ 1][j]);
      }
    }
  }
  for (int i = 0; i <= len; i++) ans[i] = 0;
  for (int i = 0; i < (1 << len); i++) {
    ans[bitcnt[i]] = add(ans[bitcnt[i]], dp[ind][i]); 
  }
  for (int i = 0; i <= len; i++) {
    printf("%d\n", ans[i]);
  }
}

int main() {
#ifdef dream_maker
  freopen("input.txt", "r", stdin);
#endif 
  for (int i = 0; i < M; i++) {
    for (int j = 1; j <= N; j++) {
      bitcnt[i] += (i >> (j - 1)) & 1;
    } 
  }
  int T; scanf("%d", &T);
  while (T--) solve();
  return 0;
} 
posted @ 2018-12-30 14:26  Dream_maker_yk  阅读(308)  评论(0编辑  收藏  举报