# BZOJ 3864 Hero meet devil 超详细超好懂题解

BZOJ 3864

## 题解

dfs(cur)
if(cur > m)
for(i: 1 -> m)
for(j: 1 -> n)
f[i][j] = max(f[i - 1][j], f[i][j - 1])
if(b[i] == a[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1)
ans[f[m][n]]++
return;
for(c in {A, T, C, G})
b[cur] = c
dfs(cur + 1)


dfs(cur, f[])
if(cur > m)
ans[f[n]]++
return;
for(c in {A, T, C, G})
for(i : 1 -> n)
newf[i] = max(f[i], newf[i - 1])
if(c == a[i]) newf[i] = max(newf[i], f[i - 1] + 1)
dfs(cur + 1, newf)


dfs(cur, s)
if(cur > m)
ans[cnt1(s)]++
return;
for(c in {A, T, C, G})
for(i : 1 -> n)
f[i] = f[i - 1] + (s >> (i - 1) & 1)
for(i : 1 -> n)
newf[i] = max(f[i], newf[i - 1])
if(c == a[i]) newf[i] = max(newf[i], f[i - 1] + 1)
for(i: 1 -> n)
t |= (f[i] - f[i - 1]) << (i - 1)
dfs(cur + 1, t)


$$s$$显然有很多重复的，每层DFS都这样算一遍非常浪费，因为这段代码中$$s$$对应的$$t$$只和$$c$$有关，不如预处理出每个$$s$$$$B[cur] == c$$时能转移到哪个状态$$t$$（预处理方法就和上面这段代码中的那部分一样）。设这个状态$$t$$$$trans[s][c]$$

dfs(cur, s)
if(cur > m)
ans[cnt1(s)]++
return;
for(c in {A, T, C, G})
dfs(cur + 1, trans[s][c])


dp[0][0] = 1
for(i : 1 -> m)
for(s: 1 -> (1 << n) - 1)
for(c in {A, T, C, G})
dp[i][trans[s][c]] += dp[i - 1][s]
for(s: 1 -> (1 << n) - 1)
ans[cnt1(s)] += dp[m][s]


## AC代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
template <class T>
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
}

const int N = 15, M = 1005, P = 1000000007;
int T, n, m, id[128], a[N];
int bcnt[1<<N], trans[1<<N][4], f[2][1<<N];
char str[N];

void init_trans(){
static int pre[N], cur[N];
for(int s = 0; s < (1 << n); s++){
if(s) bcnt[s] = bcnt[s >> 1] + (s & 1);
pre[0] = s & 1;
for(int i = 1; i < n; i++)
pre[i] = pre[i - 1] + (s >> i & 1);
for(int c = 0; c < 4; c++){
int t = 0;
cur[0] = pre[0];
if(c == a[0]) cur[0] = 1;
t |= cur[0];
for(int i = 1; i < n; i++){
cur[i] = max(cur[i - 1], pre[i]);
if(c == a[i]) cur[i] = max(cur[i], pre[i - 1] + 1);
t |= (cur[i] - cur[i - 1]) << i;
}
trans[s][c] = t;
}
}
}
void inc(int &x, int y){
x += y;
if(x >= P) x -= P;
}
void calc_f(){
int pre = 1, cur = 0;
memset(f[1], 0, sizeof(f[1]));
f[1][0] = 1;
for(int i = 0; i < m; i++){
for(int s = 0; s < (1 << n); s++)
f[cur][s] = 0;
for(int s = 0; s < (1 << n); s++)
if(f[pre][s]){
for(int c = 0; c < 4; c++)
inc(f[cur][trans[s][c]], f[pre][s]);
}
swap(pre, cur);
}
static int ans[N + 1];
for(int i = 0; i <= n; i++) ans[i] = 0;
for(int s = 0; s < (1 << n); s++)
inc(ans[bcnt[s]], f[pre][s]);
for(int i = 0; i <= n; i++)
write(ans[i]), enter;
}

int main(){

id['A'] = 0, id['T'] = 1, id['C'] = 2, id['G'] = 3;
while(T--){
scanf("%s%d", str, &m);
n = strlen(str);
for(int i = 0; i < n; i++)
a[i] = id[int(str[i])];
init_trans();
calc_f();
}

return 0;
}


posted @ 2018-05-25 08:52  胡小兔  阅读(1939)  评论(3编辑  收藏  举报