bzoj3864: Hero meet devil

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
 
题解:
我们先考虑给定T和S,如何去求他们的lcs
显然我们会设g[i][j]表示T[1..i]和S[1..j]的lcs
所以g[i][j]=max(g[i-1][j],g[i][j-1],(g[i-1][j-1]+1)*(T[i]==S[j]))
我们发现g数组有两个性质
1.g[i][j]只和上一行和这一行有关
2.对于任意的i,j,有g[i][j]-g[i][j-1]=0或1
而且这题的|S|=15,这启示我们可以将j那一维状态压缩一下,用一个二进制数存储相邻两项的差值即可
设trans[sta][c]表示T已经匹配了若干位的状态为sta,假如下一位为c,状态会变为trans[sta][c]
这个可以O(n*2n)预处理出来
然后在设f[i][sta]表示T已经匹配到了i位,状态为sta的方案数,利用一下trans数组进行转移(记得要滚动数组),这个复杂度为O(m*2n)
code:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 const int maxn=20;
 8 const int maxm=1005;
 9 const int maxstate=32800;
10 const int mod=1000000007;
11 const char dna[4]={'A','C','G','T'};
12 char s[maxn];
13 int T,n,m,lim,cnt[maxstate];
14 int cur[maxn],g[maxn],trans[maxstate][4],f[2][maxstate],ans[maxn];
15 void work(){
16     for (int sta=0;sta<lim;sta++){
17         for (int i=1;i<=n;i++) cur[i]=cur[i-1]+((sta>>(i-1))&1);
18         for (int c=0;c<4;c++){
19             for (int i=1;i<=n;i++) g[i]=0;
20             for (int i=1;i<=n;i++){
21                 g[i]=max(g[i-1],cur[i]);
22                 if (s[i]==dna[c]) g[i]=max(g[i],cur[i-1]+1);
23             }
24             int res=0;
25             for (int i=1;i<=n;i++) if (g[i]>g[i-1]) res|=(1<<(i-1));
26             trans[sta][c]=res;
27         }
28     }
29     memset(f[0],0,sizeof(f[0]));
30     f[0][0]=1;
31     for (int i=1;i<=m;i++){
32         memset(f[i&1],0,sizeof(f[i&1]));
33         for (int sta=0;sta<lim;sta++) if (f[(i-1)&1][sta])
34             for (int c=0;c<4;c++) f[i&1][trans[sta][c]]+=f[(i-1)&1][sta],f[i&1][trans[sta][c]]%=mod;
35     }
36     memset(ans,0,sizeof(ans));
37     for (int sta=0;sta<lim;sta++) ans[cnt[sta]]+=f[m&1][sta],ans[cnt[sta]]%=mod;
38     for (int i=0;i<=n;i++) printf("%d
",ans[i]);
39 }
40 int main(){
41     for (int i=1;i<32768;i++) cnt[i]=cnt[i&(i-1)]+1;
42     for (scanf("%d",&T);T;T--) scanf("%s%d",s+1,&m),n=strlen(s+1),lim=1<<n,work();
43     return 0;
44 }
原文地址:https://www.cnblogs.com/chenyushuo/p/5222444.html