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.
For each test case, the first line contains a string S. the second line contains an integer m.
T<=5
|S|<=15. m<= 1000.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = ;
const int MOD = 1e9 + ; char dic[] = "ACTG";
int add[ << ][];
int dp[][ << ];
int pre[MAXN], lcs[MAXN], ans[MAXN];
char s[MAXN];
int T, n, m; inline void update_add(int &a, int b) {
a += b;
if(a >= MOD) a -= MOD;
} void init() {
for(int state = ; state < ( << n); ++state) {
pre[] = ;
for(int i = ; i <= n; ++i) pre[i] = pre[i - ] + ((state >> (i - )) & );
for(int k = ; k < ; ++k) {
for(int i = ; i <= n; ++i) {
if(s[i] == dic[k]) lcs[i] = pre[i - ] + ;
else lcs[i] = max(lcs[i - ], pre[i]);
}
int &t = add[state][k] = ;
for(int i = ; i <= n; ++i)
t |= ((lcs[i] != lcs[i - ]) << (i - ));
}
}
} void solve() {
int *now = dp[], *next = dp[];
memset(next, , ( << n) * sizeof(int));
next[] = ;
for(int _ = ; _ < m; ++_) {
swap(now, next);
memset(next, , ( << n) * sizeof(int));
for(int state = ; state < ( << n); ++state) if(now[state])
for(int k = ; k < ; ++k)
update_add(next[add[state][k]], now[state]);
}
memset(ans, , sizeof(ans));
for(int state = ; state < ( << n); ++state) {
update_add(ans[__builtin_popcount(state)], next[state]);
}
for(int i = ; i <= n; ++i)
printf("%d\n", ans[i]);
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%s%d", s + , &m);
n = strlen(s + );
init();
solve();
}
}