CLS好早的题了。。。非常经典啊。。。
题意:给定长度为
n
的串
我们希望能够用一个
f[j][{lcs[1][j],lcs[2][j],lcs[3][j]...lcs[n][j]}]
来表示当前状态以计算答案。
一开始似乎会尝试直接用子序列来直接容斥。。。会发现这样很难搞,也不太对。。于是回到LCS的计算上看看有什么性质可以用。
LCS[i][j]=max{LCS[i−1][j],LCS[i][j−1], [S[i]==T[j]]∗(LCS[i−1][j−1]+1)}
注意到在这个DP矩阵里面,任意相邻的两格的差至多为1,这样我们就可以用一个状压将相邻格子的差搞起来,以此表示 T 串到第
而对于状态之间的转移,可以先枚举上一行的 LCS 状态,算出来之后枚举当前行会和什么字符匹配,以计算出当前行的 LCS 状态,并得到状压的状态。简单地预处理一下就可以只枚举当前行的匹配字符来快速完成转移了。
预处理的复杂度为 O(n2n) ,DP复杂度为 O(m2n) 。
感觉这东西要讲清楚有点麻烦呢。。。
丑的一B的代码:
/*
I will chase the meteor for you, a thousand times over.
Please wait for me, until I fade forever.
Just 'coz GEOTCBRL.
*/
#include <bits/stdc++.h>
using namespace std;
#define fore(i,u) for (int i = head[u] ; i ; i = nxt[i])
#define rep(i,a,b) for (int i = a , _ = b ; i <= _ ; i ++)
#define per(i,a,b) for (int i = a , _ = b ; i >= _ ; i --)
#define For(i,a,b) for (int i = a , _ = b ; i < _ ; i ++)
#define Dwn(i,a,b) for (int i = ((int) a) - 1 , _ = (b) ; i >= _ ; i --)
#define fors(s0,s) for (int s0 = (s) , _S = s ; s0 ; s0 = (s0 - 1) & _S)
#define foreach(it,s) for (__typeof(s.begin()) it = s.begin(); it != s.end(); it ++)
#define mp make_pair
#define pb push_back
#define pii pair<int , int>
#define fir first
#define sec second
#define MS(x,a) memset(x , (a) , sizeof (x))
#define gprintf(...) fprintf(stderr , __VA_ARGS__)
#define gout(x) std::cerr << #x << "=" << x << std::endl
#define gout1(a,i) std::cerr << #a << '[' << i << "]=" << a[i] << std::endl
#define gout2(a,i,j) std::cerr << #a << '[' << i << "][" << j << "]=" << a[i][j] << std::endl
#define garr(a,l,r,tp) rep (__it , l , r) gprintf(tp"%c" , a[__it] , " \n"[__it == _])
template <class T> inline void upmax(T&a , T b) { if (a < b) a = b ; }
template <class T> inline void upmin(T&a , T b) { if (a > b) a = b ; }
typedef long long ll;
const int maxn = 100007;
const int maxm = 200007;
const int maxs = 1 << 16;
const int mod = 1000000007;
const int inf = 0x7fffffff;
const double eps = 1e-7;
typedef int arr[maxn];
typedef int adj[maxm];
inline int fcmp(double a , double b) {
if (fabs(a - b) <= eps) return 0;
if (a < b - eps) return -1;
return 1;
}
inline int add(int a , int b) { return ((ll) a + b) % mod ; }
inline int mul(int a , int b) { return ((ll) a * b) % mod ; }
inline int dec(int a , int b) { return add(a , mod - b % mod) ; }
inline int Pow(int a , int b) {
int t = 1;
while (b) {
if (b & 1) t = mul(t , a);
a = mul(a , a) , b >>= 1;
}
return t;
}
#define gc getchar
#define idg isdigit
#define rd RD<int>
#define rdll RD<long long>
template <typename Type>
inline Type RD() {
char c = getchar(); Type x = 0 , flag = 1;
while (!idg(c) && c != '-') c = getchar();
if (c == '-') flag = -1; else x = c - '0';
while (idg(c = gc()))x = x * 10 + c - '0';
return x * flag;
}
inline char rdch() {
char c = gc();
while (!isalpha(c)) c = gc();
return c;
}
#undef idg
#undef gc
// beginning
#define shl(x) (1 << (x))
const char ch[] = {'A' , 'C' , 'G' , 'T'};
int n , m , all;
int bcnt[maxs] , trans[maxs][4];
int f[2][maxs];
char S[16];
void input() {
scanf("%s" , S + 1);
n = strlen(S + 1) , m = rd();
all = shl(n);
}
inline void init_trans() {
static int pre[16] , cur[16];
For (s , 0 , all) {
if (s)
bcnt[s] = bcnt[s >> 1] + (s & 1);
For (i , 0 , n) pre[i + 1] = pre[i] + (s >> i & 1);
rep (c , 0 , 3) {
int t = 0;
cur[0] = 0;
rep (i , 1 , n) {
cur[i] = max(cur[i - 1] , pre[i]);
if (S[i] == ch[c]) upmax(cur[i] , pre[i - 1] + 1);
t |= (cur[i] - cur[i - 1]) << (i - 1);
}
trans[s][c] = t;
}
}
}
#define upd(a,b) a = add(a , b)
void solve() {
int cur = 0 , nxt = 1;
init_trans();
memset(f[cur] , 0 , all << 2);
f[0][0] = 1;
For (i , 0 , m) {
memset(f[nxt] , 0 , all << 2);
For (s , 0 , all) if (f[cur][s]) For (c , 0 , 4)
upd(f[nxt][trans[s][c]] , f[cur][s]);
cur ^= 1 , nxt ^= 1;
}
static int ans[16];
rep (i , 0 , n) ans[i] = 0;
For (s , 0 , all)
upd(ans[bcnt[s]] , f[cur][s]);
rep (i , 0 , n) printf("%d\n" , ans[i]);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.txt" , "r" , stdin);
#endif
rep (T , 1 , rd()) {
input();
solve();
}
return 0;
}